WebSocket connection to UnityBase server using ubNotifier protocol.
The property supported
indicate ubNotifier
protocol is supported by server.
Class mixes an EventEmitter. After got a command from a server the event with command code
is fired.
The event flow are:
- just after webSocket connection is established
connected
event are fired, when - after server accept (verify a user credential)
accepted
are fired - here you can got a connectionID of a current connection - next is a series of protocol events (
test_command
for example ) - in case of protocol error
error
are fired - when WebSocket connection are closed
disconnected
event are fired
You do not need to connect or reconnect a WebSocket manually - UBNotifierWSProtocol recreate
a WebSocket automatically just after corresponding UBConnection fires a authorized
event.
For adminUI
instance of this class is accessible from $App.ubNotifier just after $App.launch
Usage sample:
// Server side
const WebSockets = require('@unitybase/ub/modules/web-sockets');
var notifier = WebSockets.getWSNotifier();
if (notifier) {
notifier.broadcast('test_command', {name: 'Homer', like: 'donuts'});
}
// Client side
// Will output 'Homer like donuts' after got a `test_command` from server
$App.ubNotifier.on('test_command', function(params}{
console.debug(params.name, 'like', params.like)
}
# new UBNotifierWSProtocol (connection: UBConnection)
Arguments:
connection
: UBConnectionWarning - only one instance of UBNotifierWSProtocol for a connection is valid.
Members
# supported : boolean instance
Indicate ubNotifier
protocol is supported by server.
If not supported all calls to send() will be ignored.
Methods
# sendCommand (command: string, params: *) instance
Sand a command to server
- if WS connection is not accepted yet will buffer the commands and send it just after connection is accepted
- if
ubNotifier
protocol not supported by server will do nothing
Arguments:
command
: stringparams
: *
# _createWSConnection (session) inner
Arguments:
session
:
# _onUBConnectionAuthorized (ubConnection, session) inner
Arguments:
ubConnection
:session
: