Qore WebSocketHandler Module Reference 2.0
Loading...
Searching...
No Matches
WebSocketHandler.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* WebSocketHandler.qm Copyright 2013 - 2024 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
120const WSC_Process = 0;
121
123const WSC_Continue = 1;
124
126const WSC_Break = 2;
128
130
144
145public:
148
150 bool stop_flag = False;
151
152 // wait a maximum of 1/4 second for data
153 const DefaultQueuePollingInterval = 250ms;
154
155protected:
158
160 int tid = gettid();
161
162public:
163
164private:
166 Socket sock;
167
168public:
169
171
176
177
180
181
183
185 *binary pollData();
186
187
189
192
193
195 sendEncoded(binary msg);
196
197
199
201 send(data msg);
202
203
205
207 send(data msg, int op, bool fin);
208
209
211
215 gotMessage(string msg);
216
217
219
223 gotMessage(binary msg);
224
225
227
235 int gotRawMessage(hash<WsMsgInfo> msg);
236
237
239
244
245
247
252
253
255
259
260
263
264};
265
267
288class WebSocketHandler : public HttpServer::AbstractHttpSocketHandler, public Logger::LoggerWrapper {
289
290public:
292 const DefaultSocketSendTimeout = 30s;
293
295 const DefaultHeartbeatInterval = 20s;
296
298 const DefaultHeartbeatMsg = "heartbeat";
299
301 const Options = ...;
302
303
304protected:
306 RWLock rwl();
307
309 hash<string, WebSocketConnection> ch;
310
312 softint heartbeat = DefaultHeartbeatInterval;
313
315 string heartbeat_msg = DefaultHeartbeatMsg;
316
317public:
318
319private:
321 string tlk = get_random_string(50);
322
323public:
324
326
339
340
342
347
348
350
354 setHeartbeat(softint seconds);
355
356
358
363
364
366
371
372
374
406 hash<auto> handleRequest(hash<auto> cx, hash<auto> hdr, *data b);
407
408
410
412 *list<string> getConnectionIds();
413
414
416
432 bool startImpl(softstring lid, hash<auto> cx, hash<auto> hdr, Qore::Socket sock);
433
434
436
438 bool flushIo(WebSocketConnection wsc, Socket sock);
439
440
441private:
442 bool flushIoIntern(WebSocketConnection wsc, Socket sock, bool update_tld);
443public:
444
445
447 WebSocketConnection getConnectionImpl(hash<auto> cx, hash<auto> hdr, string cid);
448
449
451 WebSocketConnection doNewConnection(hash<auto> cx, hash<auto> hdr, string cid);
452
453
456
457
459
463 sendAll(data d);
464
465
467
473 sendOne(softstring id, data d);
474
475
477 stopOne(softstring id);
478
479
480protected:
481 sendClose(Qore::Socket sock, int code, *string txtmsg);
482public:
483
484
485 static string getDataString(*data d);
486
488 logInfo(string fmt, ...);
489
490
492 logError(string fmt, ...);
493
494
496 logWarn(string fmt, ...);
497
498
500 logDebug(string fmt, ...);
501
502};
503};
this class represents a connection to a websocket client
Definition: WebSocketHandler.qm.dox.h:143
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:160
bool stop_flag
Stop connection flag.
Definition: WebSocketHandler.qm.dox.h:150
Socket sock
The socket used by the server for this connection.
Definition: WebSocketHandler.qm.dox.h:166
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:147
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
logWarn(string fmt,...)
Log to the logger.
logError(string fmt,...)
Log to the logger.
*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
RWLock rwl()
connection read-write lock
int getHeartbeat()
returns the connection heartbeat interval as a number of seconds
logDebug(string fmt,...)
Log to the logger.
logInfo(string fmt,...)
Log to the logger.
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...
bool 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...
hash< string, WebSocketConnection > ch
connection hash
Definition: WebSocketHandler.qm.dox.h:309
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:126
const WSC_Continue
If the server should continue listening (message already processed)
Definition: WebSocketHandler.qm.dox.h:123
const WSC_Process
Definition: WebSocketHandler.qm.dox.h:120
the WebSocketHandler namespace contains all the objects in the WebSocketHandler module
Definition: WebSocketHandler.qm.dox.h:115