Qore WebSocketClient Module Reference 2.2
Loading...
Searching...
No Matches
WebSocketClient.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* WebSocketClient.qm Copyright 2013 - 2023 Qore Technologies, s.r.o.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23*/
24
25// minimum required Qore version
26
27// require type definitions everywhere
28
29// enable all warnings
30
31// do not require "$" signs for vars, assume local variable scope
32
33
34
35
36
181const WsSchemes = {
182 "ws": {
183 "ssl": False,
184 },
185 "wss": {
186 "ssl": True,
187 },
188};
189
194const WSC_Process = 0;
195
197const WSC_Continue = 1;
198
200const WSC_Break = 2;
202
204const WsDefaultPort = 80;
205
207const EVENT_WS_DATA = "ws-data-event";
208
210const EVENT_WS_PING = "ws-ping-event";
211
213const EVENT_WS_PONG = "ws-pong-event";
214
216const EVENT_WS_CLOSED = "ws-closed-event";
217
219
222const MESSAGE_WS_DATA = "data";
223
225
227const MESSAGE_WS_RAW = "raw";
228
230
232const MESSAGE_WS_PING = "ping";
233
235
237const MESSAGE_WS_PONG = "pong";
238
240
256
257public:
258protected:
259 Mutex m();
260 HTTPClient hc;
261 int stop = 0;
262 Counter c();
263 code callback;
264 code raw_callback;
265 code pong_callback;
266 *code log;
267 *code errlog;
268 *code debuglog;
269 timeout timeout_ms = DefaultTimeout;
271 int tid;
273 int cid = -1;
275 *code yield;
277 string url;
278
279 static Sequence seq();
280
281public:
282
284 const DefaultTimeout = 15s;
285
287 const Version = "1.6";
288
290 const DefaultUserAgent = sprintf("Qore-WebSocketClient/%s", WebSocketClient::Version);
291
293
333 constructor(code cb, hash<auto> opts);
334
335
338
339
341 string getUrl();
342
343
345 string getSafeUrl();
346
347
350
351
353 hash<auto> getSchemes();
354
355
357
399 hash<auto> connect(*hash<auto> opts, *reference<hash<auto>> info);
400
401
403 bool isOpen();
404
405
407 disconnect(int cmd = WSCC_GoingAway);
408
409
411
421
422
424
454 nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms = 1s);
455
456
458
476 hash<auto> getUsageInfo();
477
478
480
490
491
493
509 setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data);
510
511
513
523
524
525protected:
526 disconnectUnlocked(int cmd = WSCC_GoingAway);
527public:
528
529
530protected:
531 eventLoop(code callback);
532public:
533
534
535protected:
536 receivedPong(*data msg);
537public:
538
539
540protected:
541 handlePing(hash<auto> h);
542public:
543
544
545protected:
546 logInfo(string fmt);
547public:
548
549
550protected:
551 logError(string fmt);
552public:
553
554
555protected:
556 logDebug(string fmt);
557public:
558
559
560protected:
561 sendClose(int code, *string txtmsg);
562public:
563
564
565protected:
566 hash<auto> connectUnlocked(*hash<auto> hdr0, *reference<hash<auto>> info);
567public:
568
569
571
573 ping(*data msg);
574
575
577
579 pong(*data msg);
580
581
583
588 send(string str);
589
590
592
597 send(binary bin);
598
599
601 send(data msg, int op, bool fin);
602
603};
604
606
621
622public:
624 const ConnectionScheme = <ConnectionSchemeInfo>{
625 "cls": Class::forName("WebSocketConnectionObject"),
626 "options": HttpConnection::ConnectionScheme.options,
627 };
628
630
640 constructor(string name, string description, string url, hash<auto> attributes = {}, hash<auto> options = {})
641 ;
642
643
645 string getType();
646
647
649
656 *hash<auto> getRuntimeOptions();
657
658
659protected:
660 hash<PingInfo> pingIntern(hash<PingInfo> rv);
661public:
662
663
665
675protected:
676 WebSocketClient getImpl(bool connect = True, *hash<auto> rtopts);
677public:
678
679
681
686
687
689
694
695
697protected:
699public:
700
701
703protected:
704 hash<ConnectionSchemeInfo> getConnectionSchemeInfoImpl();
705public:
706
707};
708
710
713
714public:
716 const ProviderInfo = <DataProviderInfo>{
717 "supports_children": True,
718 "supports_observable": True,
719 "supports_messages": MSG_Async,
720 "children_can_support_apis": True,
721 "constructor_options": ConstructorOptions,
722 "desc": "Allows sending and receiving WebSocket messages to and from a WebSocket server",
723 };
724
727 "connect_timeout": <DataProviderOptionInfo>{
728 "type": AbstractDataProviderTypeMap."int",
729 "desc": "connection timeout to use in milliseconds",
730 },
731 "data": <DataProviderOptionInfo>{
732 "type": AbstractDataProviderTypeMap."string",
733 "desc": "data serialization to use:\n"
734%ifndef NoJson
735 " - `json`: JSON serialization (default)\n"
736%endif
737%ifndef NoYaml
738 " - `yaml`: YAML serialization\n"
739%endif
740 " - `none`: no serialization; raw messages\n",
741 "default_value":
742%ifndef NoJson
743 "json"
744%else
745 "none"
746%endif
747 ,
748 },
749 "headers": <DataProviderOptionInfo>{
750 "type": AbstractDataProviderTypeMap."hash",
751 "desc": "headers to add in each outgoing request",
752 },
753 "http_version": <DataProviderOptionInfo>{
754 "type": AbstractDataProviderTypeMap."string",
755 "desc": "HTTP version to use (`1.0` or `1.1`, defaults to `1.1`)",
756 "default_value": "1.1",
757 },
758 "max_redirects": <DataProviderOptionInfo>{
759 "type": AbstractDataProviderTypeMap."int",
760 "desc": "maximum redirects to support",
761 },
762 "proxy": <DataProviderOptionInfo>{
763 "type": AbstractDataProviderTypeMap."string",
764 "desc": "proxy URL to use",
765 },
766 "report_pings": <DataProviderOptionInfo>{
767 "type": AbstractDataProviderTypeMap."bool",
768 "desc": "Raise events for ping message from clients",
769 "default_value": False,
770 },
771 "timeout": <DataProviderOptionInfo>{
772 "type": AbstractDataProviderTypeMap."int",
773 "desc": "transfer timeout to use in milliseconds",
774 },
775 "url": <DataProviderOptionInfo>{
776 "type": AbstractDataProviderTypeMap."string",
777 "desc": "A URL for a WebSocket connection",
778 "required": True,
779 },
780 };
781
783 const ApiClasses = {
784 "wait-for-message": Class::forName("WebSocketClientWaitForMessageDataProvider"),
785 };
786
788 const SerializationMap = ...;
789
790
791protected:
794
797
800
802 Mutex m();
803
806
808 Queue q;
809
810public:
811
813 constructor(*hash<auto> options);
814
815
816 destructor();
817
818
819 string getName();
820
821
824
825
828
829
831 Queue getQueue();
832
833
835
837protected:
839public:
840
841
843
849protected:
850 *AbstractDataProvider getChildProviderImpl(string name);
851public:
852
853
855protected:
857public:
858
859
861
865protected:
866 hash<string, hash<DataProviderMessageInfo>> getEventTypesImpl();
867public:
868
869
871
875protected:
876 hash<string, hash<DataProviderMessageInfo>> getMessageTypesImpl();
877public:
878
879
881
885protected:
886 sendMessageImpl(string message_id, auto msg, *hash<auto> send_message_options);
887public:
888
889
891protected:
892 event(*data msg);
893public:
894
895
897protected:
898 pong(*data msg);
899public:
900
901
903 auto deserializeMessage(data msg);
904
905
907 data serializeMessage(auto msg);
908
909
911 static hash<auto> getOptionsFromTemplate(hash<auto> template_options, *hash<auto> options);
912
914protected:
916public:
917
918
920 abstract notifyObservers(string msg_id, hash<auto> event);
921};
922
924
929
930public:
932 const ProviderInfo = <DataProviderInfo>{
933 "type": "WebSocketClientDataProvider",
935
937 constructor(*hash<auto> options) ;
938
939
941 constructor(hash<auto> template_options, *hash<auto> options) ;
942
943
944protected:
945 hash<DataProviderInfo> getStaticInfoImpl();
946public:
947
948};
949
951
956
957public:
959 const ProviderInfo = <DataProviderInfo>{
960 "type": "WebSocketClientDelayedDataProvider",
962
964 constructor(*hash<auto> options) ;
965
966
968 constructor(hash<auto> template_options, *hash<auto> options) ;
969
970
972
975
976
977protected:
978 hash<DataProviderInfo> getStaticInfoImpl();
979public:
980
981};
982
984class WebSocketClientEventDataType : public HashDataType {
985
986public:
989
990};
991
993class WebSocketClientWaitForMessageDataProvider : public AbstractDataProvider {
994
995public:
997 const ProviderInfo = <DataProviderInfo>{
998 "type": "WebSocketClientWaitForMessageDataProvider",
999 "supports_request": True,
1000 "desc": "Waits for a WebSocket message to be received from the server within a timeout period and "
1001 "returns the message if one is received in the timeout perdiod, or an exception is thrown",
1002 };
1003
1006
1008 const ResponseType = AbstractDataProviderTypeMap."any";
1009
1010protected:
1012
1013public:
1014
1017
1018
1021
1022
1024 string getName();
1025
1026
1028
1033protected:
1034 auto doRequestImpl(auto req, *hash<auto> request_options);
1035public:
1036
1037
1039
1041protected:
1042 *AbstractDataProviderType getRequestTypeImpl();
1043public:
1044
1045
1047
1049protected:
1050 *AbstractDataProviderType getResponseTypeImpl();
1051public:
1052
1053
1055 hash<DataProviderInfo> getStaticInfoImpl();
1056
1057};
1058
1061
1062public:
1064 const Fields = {
1065 "timeout": {
1066 "type": IntType,
1067 "desc": "The number of milliseconds to wait for an incoming message; after this point, if no "
1068 "WebSocket message has been received, an exception will be thrown",
1069 "required": True,
1070 "default_value": 10000,
1071 },
1072 };
1073
1076
1077};
1078
1080class WebSocketClientDataProviderFactory : public AbstractDataProviderFactory {
1081
1082public:
1083protected:
1085 static Class cls = new Class("WebSocketClientDelayedDataProvider");
1086
1088 const FactoryInfo = <DataProviderFactoryInfo>{
1089 "name": "wsclient",
1090 "desc": "WebSocket client data provider factory",
1091 "children_can_support_observers": True,
1092 "children_can_support_messages": True,
1093 };
1094
1095public:
1096
1098
1100protected:
1101 hash<DataProviderFactoryInfo> getInfoImpl();
1102public:
1103
1104
1106
1108protected:
1109 hash<DataProviderInfo> getProviderInfoImpl();
1110public:
1111
1112
1114protected:
1116public:
1117
1118};
1119};
Event- and message-based data provider for WebSocket client-side events.
Definition: WebSocketClient.qm.dox.h:712
Queue getQueue()
Returs the child message queue.
constructor(*hash< auto > options)
Creates the object from constructor options.
const ConstructorOptions
Constructor options.
Definition: WebSocketClient.qm.dox.h:726
unsupportedSerialization()
Throws an UNSUPPORTED-SERIALIZATION error.
deregister()
Dergister a child data provider.
string serialization
Data serialization.
Definition: WebSocketClient.qm.dox.h:796
const ProviderInfo
Provider info.
Definition: WebSocketClient.qm.dox.h:716
bool report_pings
report pings
Definition: WebSocketClient.qm.dox.h:799
WebSocketClient client
The web socket client itself.
Definition: WebSocketClient.qm.dox.h:793
*list< string > getChildProviderNamesImpl()
Returns a list of child data provider names.
hash< string, hash< DataProviderMessageInfo > > getEventTypesImpl()
Returns a hash of all supported event types.
WebSocketClientWaitForMessageDataProvider child
Wait for message API data provider.
Definition: WebSocketClient.qm.dox.h:805
sendMessageImpl(string message_id, auto msg, *hash< auto > send_message_options)
Sends a message from message-capable data providers.
const SerializationMap
Set of serialization options as a hash.
Definition: WebSocketClient.qm.dox.h:788
auto deserializeMessage(data msg)
Deserializes the message.
event(*data msg)
Called when a WebSocket event arrives.
abstract notifyObservers(string msg_id, hash< auto > event)
For WS event notifications.
Mutex m()
Mutex for wait for message child data provider handling.
data serializeMessage(auto msg)
Serializes the message for sending.
const ApiClasses
Registered API classes.
Definition: WebSocketClient.qm.dox.h:783
Queue q
Child message queue.
Definition: WebSocketClient.qm.dox.h:808
*AbstractDataProvider getChildProviderImpl(string name)
Returns the given child provider or NOTHING if the given child is unknown.
hash< string, hash< DataProviderMessageInfo > > getMessageTypesImpl()
Returns the description of an outbound message, if any.
static hash< auto > getOptionsFromTemplate(hash< auto > template_options, *hash< auto > options)
Returns options from a template.
start()
Connects and starts receiving messages.
The WebSocket data provider factory.
Definition: WebSocketClient.qm.dox.h:1080
static Class cls
Data provider type info.
Definition: WebSocketClient.qm.dox.h:1085
hash< DataProviderInfo > getProviderInfoImpl()
Returns static provider information.
const FactoryInfo
Factory info.
Definition: WebSocketClient.qm.dox.h:1088
Class getClassImpl()
Returns the class for the data provider object.
hash< DataProviderFactoryInfo > getInfoImpl()
Returns static factory information without provider_info.
Event-based data provider for WebSocket messaging.
Definition: WebSocketClient.qm.dox.h:928
constructor(hash< auto > template_options, *hash< auto > options)
Creates the object from constructor + template options.
const ProviderInfo
Provider info.
Definition: WebSocketClient.qm.dox.h:932
constructor(*hash< auto > options)
Creates the object from constructor options.
Event-based data provider for WebSocket messaging.
Definition: WebSocketClient.qm.dox.h:955
const ProviderInfo
Provider info.
Definition: WebSocketClient.qm.dox.h:959
observersReady()
Called when all observers have been added to the object.
constructor(hash< auto > template_options, *hash< auto > options)
Creates the object from constructor + template options.
constructor(*hash< auto > options)
Creates the object from constructor options.
WebSocket event data description.
Definition: WebSocketClient.qm.dox.h:984
bool isOpen()
returns True if the connection is currently open and active, False if not
string getUrl()
returns the URL given in the constructor
setEventQueue()
Removes any Queue object so that socket events are no longer added to the Queue.
constructor(code cb, hash< auto > opts)
creates the object and optionally sets logging targets
clearStats()
Clears performance statistics.
*code yield
callable object to yield the current thread's execution
Definition: WebSocketClient.qm.dox.h:275
nothing clearWarningQueue()
Removes any warning Queue object from the Socket.
nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, auto arg, timeout min_ms=1s)
Sets a Queue object to receive socket warnings.
destructor()
disconnects the connection and stops the event thread if connected
setEventQueue(Qore::Thread::Queue queue, auto arg, *bool with_data)
Sets a Queue object to receive socket events.
int tid
TID of the event thread.
Definition: WebSocketClient.qm.dox.h:271
pong(*data msg)
Sends a PONG message to the server as a unidirectional keep-alive message.
hash< auto > getSchemes()
returns a hash of URL scheme information for URL schemes supported by this object
string getSafeUrl()
Returns a "safe" URL, without any password info.
ping(*data msg)
Sends a PING message to the server as a unidirectional keep-alive message.
const Version
module version
Definition: WebSocketClient.qm.dox.h:287
send(string str)
Sends string data over the web socket with timeout specified in options; if any errors occur,...
int getConnectionId()
returns the unique connection ID
string url
URL string.
Definition: WebSocketClient.qm.dox.h:277
disconnect(int cmd=WSCC_GoingAway)
disconnect with the given close code
hash< auto > getUsageInfo()
Returns performance statistics for the socket.
send(data msg, int op, bool fin)
pushes an unencoded message on the connection's message queue; the message will be encoded with WebSo...
send(binary bin)
Sends binary data over the web socket with timeout specified in options; if any errors occur,...
hash< auto > connect(*hash< auto > opts, *reference< hash< auto > > info)
connects to the websocket server
WebSocket client wait for message from server API data provider.
Definition: WebSocketClient.qm.dox.h:993
const RequestType
Request type.
Definition: WebSocketClient.qm.dox.h:1005
auto doRequestImpl(auto req, *hash< auto > request_options)
Makes a request and returns the response.
hash< DataProviderInfo > getStaticInfoImpl()
Returns data provider static info.
string getName()
Returns the data provider name.
const ProviderInfo
Provider info.
Definition: WebSocketClient.qm.dox.h:997
*AbstractDataProviderType getResponseTypeImpl()
Returns the description of a response message, if this object represents a response message.
const ResponseType
Response type.
Definition: WebSocketClient.qm.dox.h:1008
constructor(WebSocketClientDataProviderBase base)
Creates the object.
*AbstractDataProviderType getRequestTypeImpl()
Returns the description of a successful request message, if any.
Data type for delete order calls.
Definition: WebSocketClient.qm.dox.h:1060
const Fields
Field descriptions.
Definition: WebSocketClient.qm.dox.h:1064
class for websocket connections; returns an object of class WebSocketClient for receiving websocket e...
Definition: WebSocketClient.qm.dox.h:620
hash< ConnectionSchemeInfo > getConnectionSchemeInfoImpl()
Returns the ConnectionSchemeInfo hash for this object.
constructor(string name, string description, string url, hash< auto > attributes={}, hash< auto > options={})
creates the WebSocketConnectionObject connection object
const ConnectionScheme
Connection entry info.
Definition: WebSocketClient.qm.dox.h:624
setChildCapabilities()
Sets child data provider capabilities.
DataProvider::AbstractDataProvider getDataProvider()
returns an WebSocketClientDataProvider object for this connection
bool hasDataProvider()
returns True, as the connection returns a data provider with the getDataProvider() method
WebSocketClient getImpl(bool connect=True, *hash< auto > rtopts)
returns a WebSocketClient object
*hash< auto > getRuntimeOptions()
returns runtime options
const WSC_Break
If the client should stop listening (message already processed)
Definition: WebSocketClient.qm.dox.h:200
const WSC_Continue
If the client should continue listening (message already processed)
Definition: WebSocketClient.qm.dox.h:197
const WSC_Process
Definition: WebSocketClient.qm.dox.h:194
the WebSocketClient namespace contains all the definitions in the WebSocketClient module
Definition: WebSocketClient.qm.dox.h:179
const MESSAGE_WS_RAW
WebSocket raw data message type.
Definition: WebSocketClient.qm.dox.h:227
const EVENT_WS_DATA
WebSocket data event constant.
Definition: WebSocketClient.qm.dox.h:207
const MESSAGE_WS_PONG
WebSocket pong message type.
Definition: WebSocketClient.qm.dox.h:237
const EVENT_WS_PING
WebSocket ping event constant.
Definition: WebSocketClient.qm.dox.h:210
const MESSAGE_WS_DATA
WebSocket data message type.
Definition: WebSocketClient.qm.dox.h:222
const WsSchemes
known websocket schemes
Definition: WebSocketClient.qm.dox.h:181
const EVENT_WS_CLOSED
WebSocket connection closed event constant.
Definition: WebSocketClient.qm.dox.h:216
const WsDefaultPort
default port for connections
Definition: WebSocketClient.qm.dox.h:204
const EVENT_WS_PONG
WebSocket pong event constant.
Definition: WebSocketClient.qm.dox.h:213
const MESSAGE_WS_PING
WebSocket ping message type.
Definition: WebSocketClient.qm.dox.h:232