Qorus Integration Engine®  5.1.31_git
OMQ::AbstractServiceWebSocketHandler Class Reference

this class is used to allow Qorus services to provide dedicated WebSocket server services to external clients More...

Inherits WebSocketHandler::WebSocketHandler OMQ::AbstractServiceHttpHandler.

Private:Internal Member Methods

 constructor (string path, HttpServer::AbstractAuthenticator auth=new DefaultQorusRBACAuthenticator(), bool path_is_regex=False)
 creates the object with the given arguments More...
 
hash< HttpResponseInfo > handleRequest (HttpListenerInterface listener, Socket s, hash< auto > cx, hash< auto > hdr, *data body)
 this method ensures that thread-local data is set properly
 
OMQ::QorusWebSocketConnection getConnectionImpl (hash< auto > cx, hash< auto > hdr, string cid)
 called when a connection is established; the default implementation creates an OMQ::QorusWebSocketConnection object
 

Detailed Description

this class is used to allow Qorus services to provide dedicated WebSocket server services to external clients

If any listeners are added to the AbstractServiceRestHandler object, then the URL set in the object is only bound

to the new listeners specified in this object. Otherwise, the URL is bound to all listeners in the system HTTP server.

Note
When used in Stateless services, HTTP and WebSocket handlers are identified by a unique ID that is generated from the options used to create the object. To ensure that all stateless services use the same ID, you must ensure that the same options are used in each service's bindHttp() call. Failure to do this, for example my using a randomly-generated strings in the constructor, will cause runtime errors.

Member Function Documentation

◆ constructor()

OMQ::AbstractServiceWebSocketHandler::constructor ( string  path,
HttpServer::AbstractAuthenticator  auth = new DefaultQorusRBACAuthenticator(),
bool  path_is_regex = False 
)
inlineprivate:internal

creates the object with the given arguments

Parameters
paththe URI path match on incoming requests
auththe AbstractAuthenticator for the object; if no argument is passed, then the system RBAC authenticator is used; meaning that Qorus application users are required to connect, and the users must have the OMQ::QR_LOGIN role to connect as well
path_is_regexif True then path is interpreted as a regular expression; note that matching requests based on regular expressions is much slower than a simple string match
Python Example
from svc import QorusService
from qore.WebSocketHandler import WebSocketHandler, WebSocketConnection
from qore.json import make_json
from datetime import timedelta
quit = False
ExitPollInterval = timedelta(seconds = 1)
class MyWebSocketConnection(WebSocketConnection):
def __init__(self, handler):
super(MyWebSocketConnection, self).__init__(handler)
def gotMessage(self, msg):
UserApi.logInfo("got msg: %y", msg)
self.send("echo " + msg)
class MyWebSocketHandler(OMQ.AbstractServiceWebSocketHandler):
def __init__(self):
super(MyWebSocketHandler, self).__init__("my-websocket")
svcapi.startThread(self.eventListener)
def eventListener(self):
global quit
global ExitPollInterval
min_id = 1
while not quit:
eh = svcapi.waitForEvents(min_id, ExitPollInterval)
if 'shutdown' in eh:
UserApi.logInfo("system is shutting down; stopping event thread")
break
min_id = eh['lastid'] + 1
if not 'events' in eh:
continue
json = make_json(eh['events'])
self.sendAll(json)
def getConnectionImpl(self, cx, hdr, cid):
return MyWebSocketConnection(self)
class MyPythonWebSocketService(QorusService):
# "init" is a registered service method in the corresponding YAML metadata file
def init(self):
# "stop" is a registered service method in the corresponding YAML metadata file
def stop(self):
global quit
quit = True
this class is used to allow Qorus services to provide dedicated WebSocket server services to external...
Definition: AbstractServiceHttpHandler.qc:827
string make_json(any data, *int format, *string encoding)
Qore Example
class MyWebSocketHandler inherits OMQ::AbstractServiceWebSocketHandler {
constructor() : AbstractServiceWebSocketHandler("my-websocket") {
}
WebSocketHandler::WebSocketConnection getConnectionImpl(hash<auto> cx, hash<auto> hdr, string cid) {
return new WebSocketHandler::WebSocketConnection(self);
}
}
main Qorus namespace
Definition: QorusRbacAuth.qm:25
Since
Qorus 5.0 the path argument is assumed to be a regular string for faster performance

The documentation for this class was generated from the following file: