Qore WebSocketHandler Module Reference 1.5.4
|
the main web socket handler class More...
#include <WebSocketHandler.qm.dox.h>
Public Member Methods | |
constructor (*HttpServer::AbstractAuthenticator auth, *hash< auto > opts) | |
create the object optionally with the given AbstractAuthenticator More... | |
deregisterConnectionImpl (WebSocketConnection wsc) | |
called when the connection terminates; the default implementation does nothing | |
WebSocketConnection | doNewConnection (hash< auto > cx, hash< auto > hdr, string cid) |
called when a connection is established; registers the new connection internally | |
bool | flushIo (WebSocketConnection wsc, Socket sock) |
Flushes I/O in the connection queue. More... | |
*list< string > | getConnectionIds () |
get list of socket connection ids | |
WebSocketConnection | getConnectionImpl (hash< auto > cx, hash< auto > hdr, string cid) |
called when a connection is established; the default implementation creates a WebSocketConnection object | |
int | getHeartbeat () |
returns the connection heartbeat interval as a number of seconds More... | |
string | getHeartbeatMessage () |
returns the heartbeat message as a string More... | |
hash< auto > | handleRequest (hash< auto > cx, hash< auto > hdr, *data b) |
called by the HTTP server to handle incoming HTTP requests More... | |
logDebug (string fmt) | |
default implementation is empty | |
logError (string fmt) | |
default implementation is empty | |
logInfo (string fmt) | |
default implementation is empty | |
sendAll (data d) | |
sends a message to all connected clients More... | |
sendOne (softstring id, data d) | |
sends a message to the given connection ID More... | |
setHeartbeat (softint seconds) | |
sets the heartbeat interval as a number of seconds More... | |
setHeartbeatMessage (string msg) | |
sets the heartbeat message More... | |
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 should be established More... | |
stopOne (softstring id) | |
stop given connection ID | |
Private Member Methods | |
RWLock | rwl () |
connection read-write lock | |
Private Attributes | |
hash< string, WebSocketConnection > | ch |
connection hash | |
softint | heartbeat = DefaultHeartbeatInterval |
heartbeat interval in seconds | |
string | heartbeat_msg = DefaultHeartbeatMsg |
heartbeat message | |
Private:Internal Attributes | |
string | tlk = get_random_string(50) |
Thread-local key for I/O check. | |
the main web socket handler class
connections are represented by object descended from WebSocketConnection. WebSocketHandler::WebSocketHandler::getConnectionImpl() returns a suitable object for each connection, this method can be overridden in subclasses to return a custom object for each connection if required.
Websocket connections are identified by their HTTP connection ID as passed in WebSocketHandler::handleRequest() in the "cx.id"
argument when the connection is first established.
WebSocketHandler::WebSocketHandler::constructor | ( | *HttpServer::AbstractAuthenticator | auth, |
*hash< auto > | opts | ||
) |
create the object optionally with the given AbstractAuthenticator
auth | the authentication object to use to authenticate connections (see AbstractAuthenticator); if no AbstractAuthenticator object is passed, then by default no authentication will be required |
opts | options for the object with the following optional keys:
|
WEBSOCKETHANDLER-OPTION-ERROR | unknown or invalid option |
bool WebSocketHandler::WebSocketHandler::flushIo | ( | WebSocketConnection | wsc, |
Socket | sock | ||
) |
Flushes I/O in the connection queue.
int WebSocketHandler::WebSocketHandler::getHeartbeat | ( | ) |
returns the connection heartbeat interval as a number of seconds
string WebSocketHandler::WebSocketHandler::getHeartbeatMessage | ( | ) |
returns the heartbeat message as a string
hash< auto > WebSocketHandler::WebSocketHandler::handleRequest | ( | hash< auto > | cx, |
hash< auto > | hdr, | ||
*data | b | ||
) |
called by the HTTP server to handle incoming HTTP requests
To accept a dedicated connection; make sure the return value hash's "code"
key is 101 (i.e. "Switching Protocols"
) and the "close"
key is not False
cx | call context hash; this hash will have the following keys:
|
hdr | incoming header hash; all keys will be converted to lower-case, additionally the following keys will be present:
|
b | message body, if any |
"code"
is 101 (ie "Switching Protocols"
) and the "close"
key is not False:"code"
: the HTTP return code (see HttpServer::HttpCodes) (101 "Switching Protocols"
to accept the dedicated connection, in which case the start() method will be called)"body"
: the message body to return in the response"close"
: (optional) set this key to True if the connection should be unconditionally closed when the handler returns"hdr"
: (optional) set this key to a hash of extra header information to be returned with the responseWebsocket connections are identified by their HTTP connection ID as passed in "cx.id"
WebSocketHandler::WebSocketHandler::sendAll | ( | data | d | ) |
sends a message to all connected clients
Messages are automatically encoded with WebSocketUtil::ws_encode_message() before sending.
WebSocketHandler::WebSocketHandler::sendOne | ( | softstring | id, |
data | d | ||
) |
sends a message to the given connection ID
Websocket connections are identified by their HTTP connection ID as passed in WebSocketHandler::handleRequest() in the "cx.id"
argument when the connection is first established.
Messages are automatically encoded with WebSocketUtil::ws_encode_message() before sending.
WebSocketHandler::WebSocketHandler::setHeartbeat | ( | softint | seconds | ) |
sets the heartbeat interval as a number of seconds
seconds | the heartbeat interval as a number of seconds |
WebSocketHandler::WebSocketHandler::setHeartbeatMessage | ( | string | msg | ) |
sets the heartbeat message
msg | the string payload to send with heartbeat messages; an empty string means no message |
WebSocketHandler::WebSocketHandler::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 should be established
This method should not return until the connection is closed or the stop() method is called
lid | the unique HTTP listener ID |
cx | call context hash; this hash will have the following keys:
|
hdr | a hash of headers in the request |
sock | the Socket object for the dedicated connection to the client |