The interface for your web socket connection
- Source:
Members
(static) isConnected
Boolean value set to true by the internal WebSocket.onopen listener, and set to false again by the internal WebSocket.onclose listener
Properties:
Type | Description |
---|---|
boolean |
- Source:
Methods
(static) addHandlers(event, handler)
Add additional handlers for the web socket events. Note that the 'message' event takes the whole event object and not only the event.message as opposed to the setEventHandler
Parameters:
Name | Type | Description |
---|---|---|
event |
string | The web socket event |
handler |
function | The web socket event handler |
- Source:
(static) connect()
Creates a new WebSocket object and sets the handlers for the web socket events
- Source:
(static) send(message)
Parameters:
Name | Type | Description |
---|---|---|
message |
string | object | The message to send to the server. The function tries to stringify the message from JSON and sends the string message if it fails |
- Source:
(static) setDefaultHandler(defaultHandler)
Set the default handler for all events. If an event don't have a handler, the default handler will be called
Parameters:
Name | Type | Description |
---|---|---|
defaultHandler |
function |
- Source:
Throws:
Invalid handler exception
(static) setEventHandler(event, handler)
Sets a web socket event handler. Note that the 'message' handler takes the event.message string and tries to JSON parse it before it calls the handler
Parameters:
Name | Type | Description |
---|---|---|
event |
'open' | 'close' | 'message' | 'error' | The web socket event to set handler on |
handler |
function | The handler for the provided event |
- Source:
Throws:
Will throw an exception if unknown event or if handler is not a function
(static) setEventHandlers(handlerObject)
This function takes an object and sets the web socket event handlers for each key=event value=handler
Parameters:
Name | Type | Description |
---|---|---|
handlerObject |
object | The handler object |
- Source:
- See:
-
- setEventHandler
Throws:
Will throw an error if supplied object is not valid
(static) setHost(host, autoConnectopt)
Sets the hostname for the socket connection
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
host |
string | The url to the host to connect to |
|
autoConnect |
boolean |
<optional> |
If this flag is set the connection will be opened after this function has been called |
- Source:
Example
cws.setHost('wss://example.com');