Qore ServerSentEventHandler Module Reference 1.0.1
Loading...
Searching...
No Matches
ServerSentEventHandler.qc.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* ServerSentEventHandler.qc Copyright 2025 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
26namespace ServerSentEventHandler {
28
44class ServerSentEventHandler : public HttpServer::AbstractHttpSocketHandler, public Logger::LoggerWrapper {
45
46public:
48 const DefaultSocketSendTimeout = 30s;
49
51 const DefaultHeartbeatInterval = 20;
52
54 const DefaultHeartbeatMsg = ":hb\n\n";
55
57 const Options = ...;
58
59
61 const SseResponseHeader = ...;
62
63
65 const DataSerializationSupport = ...;
66
67
68protected:
70 RWLock rwl();
71
74
77
79 hash<string, ServerSentEventConnection> ch;
80
82 int heartbeat = DefaultHeartbeatInterval;
83
85 string heartbeat_msg = DefaultHeartbeatMsg;
86
87public:
88
89private:
91 string tlk = get_random_string(50);
92
93public:
94
96
107 constructor(*HttpServer::AbstractAuthenticator auth, *hash<auto> opts) ;
108
109
111 constructor(hash<auto> opts) ;
112
113
115
118
119
121
123 setHeartbeat(softint seconds);
124
125
127
129 setHeartbeat(date seconds);
130
131
133
164 hash<HttpResponseInfo> handleRequest(hash<auto> cx, hash<auto> hdr, *data b);
165
166
168
170 *list<string> getConnectionIds();
171
172
174
190 bool startImpl(softstring lid, hash<auto> cx, hash<auto> hdr, Qore::Socket sock);
191
192
194
196 bool flushIo(ServerSentEventConnection conn, Socket sock);
197
198
200 string encodeMessage(hash<SseMessageInfo> msg);
201
202
204 static string serializeJson(auto v);
205
207 static string serializeDefault(auto v);
208
210 ServerSentEventConnection getConnectionImpl(Socket sock, hash<auto> cx, hash<auto> hdr, string cid);
211
212
214 ServerSentEventConnection doNewConnection(Socket sock, hash<auto> cx, hash<auto> hdr, string cid);
215
216
219
220
223
224
226
230 sendAll(hash<SseMessageInfo> msg);
231
232
234
240 sendOne(softstring id, hash<SseMessageInfo> msg);
241
242
244 stopOne(softstring id);
245
246
248 logInfo(string fmt, ...);
249
250
252 logError(string fmt, ...);
253
254
256 logWarn(string fmt, ...);
257
258
260 logDebug(string fmt, ...);
261
262
264protected:
266public:
267
268
270protected:
272public:
273
274
276protected:
277 sendHeartbeatMsg(Socket sock);
278public:
279
280
282protected:
284public:
285
286
288protected:
289 constructorInit(*hash<auto> opts);
290public:
291
292
293private:
294 bool flushIoIntern(ServerSentEventConnection conn, Socket sock, bool update_tld);
295public:
296
297};
298};
this class represents a connection to an HTTP client
Definition ServerSentEventConnection.qc.dox.h:37
constructor(*HttpServer::AbstractAuthenticator auth, *hash< auto > opts)
Create the object optionally with the given AbstractAuthenticator.
removeConnectionLocked(string cid)
Called when a connection is removed from the connection map.
setHeartbeat(date seconds)
sets the heartbeat interval as a number of seconds
sendOne(softstring id, hash< SseMessageInfo > msg)
sends a message to the given connection ID
static string serializeDefault(auto v)
Default value serialization.
constructorInit(*hash< auto > opts)
Common constructor initialization.
ServerSentEventConnection getConnectionImpl(Socket sock, hash< auto > cx, hash< auto > hdr, string cid)
Called when a connection is established; the default implementation creates a ServerSentEventConnecti...
string serialization
The value of the serialization option.
Definition ServerSentEventHandler.qc.dox.h:73
sendHeartbeatMsg(Socket sock)
Send the default heartbeat message.
sendAll(hash< SseMessageInfo > msg)
sends a message to all connected clients
logError(string fmt,...)
Log to the logger.
logDebug(string fmt,...)
Log to the logger.
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...
RWLock rwl()
connection read-write lock
ServerSentEventConnection doNewConnection(Socket sock, hash< auto > cx, hash< auto > hdr, string cid)
Called when a connection is established; registers the new connection internally.
static string serializeJson(auto v)
JSON value serialization.
constructor(hash< auto > opts)
Create the object optionally with the given optiond.
*list< string > getConnectionIds()
get list of socket connection ids
int getHeartbeat()
returns the connection heartbeat interval as a number of seconds
connectionRegisteredImpl(ServerSentEventConnection conn)
Called when the connection is registered; the default implementation does nothing.
hash< HttpResponseInfo > handleRequest(hash< auto > cx, hash< auto > hdr, *data b)
called by the HTTP server to handle incoming HTTP requests
logWarn(string fmt,...)
Log to the logger.
bool flushIo(ServerSentEventConnection conn, Socket sock)
Flushes I/O in the connection queue.
hash< string, ServerSentEventConnection > ch
connection hash
Definition ServerSentEventHandler.qc.dox.h:79
registerNewConnectionIntern(string cid, ServerSentEventConnection rv)
Called while holding the write lock to register the new connection.
code serializer
Value serialization code.
Definition ServerSentEventHandler.qc.dox.h:76
setHeartbeat(softint seconds)
sets the heartbeat interval as a number of seconds
deregisterConnectionImpl(ServerSentEventConnection conn)
Called when the connection terminates; the default implementation does nothing.
replayEventsSince(ServerSentEventConnection conn, auto id)
Resend all events since the given event.
string encodeMessage(hash< SseMessageInfo > msg)
Encode a message for sending.
logInfo(string fmt,...)
Log to the logger.
stopOne(softstring id)
stop given connection ID
the ServerSentEventHandler namespace contains all the objects in the ServerSentEventHandler module
Definition ServerSentEventConnection.qc.dox.h:26