Qore WebSocketHandler Module Reference 1.5.4
Loading...
Searching...
No Matches
WebSocketHandler.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* WebSocketHandler.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 Qore version
26
27// require type definitions everywhere
28
29// enable all warnings
30
31
32
33
117const WSC_Process = 0;
118
120const WSC_Continue = 1;
121
123const WSC_Break = 2;
125
127
141
142public:
145
147 bool stop_flag = False;
148
149 // wait a maximum of 1/4 second for data
150 const DefaultQueuePollingInterval = 250ms;
151
152protected:
155
157 int tid = gettid();
158
159public:
160
161private:
163 Socket sock;
164
165public:
166
168
173
174
177
178
180
182 *binary pollData();
183
184
186
189
190
192 sendEncoded(binary msg);
193
194
196
198 send(data msg);
199
200
202
204 send(data msg, int op, bool fin);
205
206
208
212 gotMessage(string msg);
213
214
216
220 gotMessage(binary msg);
221
222
224
232 int gotRawMessage(hash<WsMsgInfo> msg);
233
234
236
241
242
244
249
250
252
256
257
260
261};
262
264
286
287public:
289 const DefaultSocketSendTimeout = 30s;
290
292 const DefaultHeartbeatInterval = 20s;
293
295 const DefaultHeartbeatMsg = "heartbeat";
296
298 const Options = ...;
299
300
301protected:
303 RWLock rwl();
304
306 hash<string, WebSocketConnection> ch;
307
309 softint heartbeat = DefaultHeartbeatInterval;
310
312 string heartbeat_msg = DefaultHeartbeatMsg;
313
314public:
315
316private:
318 string tlk = get_random_string(50);
319
320public:
321
323
336
337
339
344
345
347
351 setHeartbeat(softint seconds);
352
353
355
360
361
363
368
369
371
403 hash<auto> handleRequest(hash<auto> cx, hash<auto> hdr, *data b);
404
405
407
409 *list<string> getConnectionIds();
410
411
413
429 startImpl(softstring lid, hash<auto> cx, hash<auto> hdr, Qore::Socket sock);
430
431
433
435 bool flushIo(WebSocketConnection wsc, Socket sock);
436
437
438private:
439 bool flushIoIntern(WebSocketConnection wsc, Socket sock, bool update_tld);
440public:
441
442
444 WebSocketConnection getConnectionImpl(hash<auto> cx, hash<auto> hdr, string cid);
445
446
448 WebSocketConnection doNewConnection(hash<auto> cx, hash<auto> hdr, string cid);
449
450
453
454
456
458 sendAll(data d);
459
460
462
468 sendOne(softstring id, data d);
469
470
472 stopOne(softstring id);
473
474
475protected:
476 sendClose(Qore::Socket sock, int code, *string txtmsg);
477public:
478
479
480 static string getDataString(*data d);
481
483 logInfo(string fmt);
484
485
487 logError(string fmt);
488
489
491 logDebug(string fmt);
492
493};
494};
this class represents a connection to a websocket client
Definition: WebSocketHandler.qm.dox.h:140
registered()
Called when the connection has been registered in handler and accept sending messages.
setSocket(Socket sock)
Sets the socket used by the server for the connection.
Qore::Thread::Queue queue()
the Queue object stores messages to be sent to the server
int tid
The TID of the I/O thread.
Definition: WebSocketHandler.qm.dox.h:157
bool stop_flag
Stop connection flag.
Definition: WebSocketHandler.qm.dox.h:147
Socket sock
The socket used by the server for this connection.
Definition: WebSocketHandler.qm.dox.h:163
sendEncoded(binary msg)
Pushes an already-encoded message on the connection's message queue.
gotMessage(binary msg)
This method is called when messages from the client are received.
gotMessage(string msg)
This method is called when messages from the client are received.
constructor(WebSocketHandler handler)
the constructor is called when a new connection is made by a websocket client
int gotRawMessage(hash< WsMsgInfo > msg)
This method is called when a message is received.
*binary pollDataImmediate()
This method is called to poll for messages from the client to send from the server.
connectionClosed()
This method is called when the connection is closed.
send(data msg, int op, bool fin)
Pushes an unencoded message on the connection's message queue.
send(data msg)
Pushes an unencoded message on the connection's message queue.
*binary pollData()
This method is called to poll for messages from the client to send from the server.
stop()
force closing socket related to connection
WebSocketHandler handler
The parent handler object.
Definition: WebSocketHandler.qm.dox.h:144
bool flushIo(WebSocketConnection wsc, Socket sock)
Flushes I/O in the connection queue.
constructor(*HttpServer::AbstractAuthenticator auth, *hash< auto > opts)
create the object optionally with the given AbstractAuthenticator
deregisterConnectionImpl(WebSocketConnection wsc)
called when the connection terminates; the default implementation does nothing
*list< string > getConnectionIds()
get list of socket connection ids
string getHeartbeatMessage()
returns the heartbeat message as a string
setHeartbeat(softint seconds)
sets the heartbeat interval as a number of seconds
setHeartbeatMessage(string msg)
sets the heartbeat message
logDebug(string fmt)
default implementation is empty
RWLock rwl()
connection read-write lock
logError(string fmt)
default implementation is empty
int getHeartbeat()
returns the connection heartbeat interval as a number of seconds
sendOne(softstring id, data d)
sends a message to the given connection ID
WebSocketConnection getConnectionImpl(hash< auto > cx, hash< auto > hdr, string cid)
called when a connection is established; the default implementation creates a WebSocketConnection obj...
logInfo(string fmt)
default implementation is empty
hash< string, WebSocketConnection > ch
connection hash
Definition: WebSocketHandler.qm.dox.h:306
startImpl(softstring lid, hash< auto > cx, hash< auto > hdr, Qore::Socket sock)
called from the HTTP server after the handleRequest() method indicates that a dedicated connection sh...
WebSocketConnection doNewConnection(hash< auto > cx, hash< auto > hdr, string cid)
called when a connection is established; registers the new connection internally
sendAll(data d)
sends a message to all connected clients
hash< auto > handleRequest(hash< auto > cx, hash< auto > hdr, *data b)
called by the HTTP server to handle incoming HTTP requests
stopOne(softstring id)
stop given connection ID
const WSC_Break
If the server should stop listening (message already processed)
Definition: WebSocketHandler.qm.dox.h:123
const WSC_Continue
If the server should continue listening (message already processed)
Definition: WebSocketHandler.qm.dox.h:120
const WSC_Process
Definition: WebSocketHandler.qm.dox.h:117
the WebSocketHandler namespace contains all the objects in the WebSocketHandler module
Definition: WebSocketHandler.qm.dox.h:112