Class: module:SocketModelBase

module:SocketModelBase

new module:SocketModelBase(skipLocalModel)

Constructor for Socket models.
Parameters:
Name Type Description
skipLocalModel boolean Controls constructor call for LocalModel.

Methods

activate()

Activates this socket model by opening the WS connection and registering event handlers. Connection is not ready for use until method onOpen is called.

close()

Closes the underlying web socket connection.

deactivate()

Deactivates this socket model by closing the web socket connection.

getUrl() → {string}

Returns underlying URL for this model. If the URL is relative, it is absolutized to ensure it starts with "ws:".
Returns:
URL for model.
Type
string

onClose()

Event handler called if the connection is closed. Attempting to send messages after this handler is called will result in an error.

onError(error)

Default error handler for this model. Outputs error message to console, if available.
Parameters:
Name Type Description
error string Error message.

onMessage(message)

Event handler called when a message is received. By default, it deserializes the message into the model.
Parameters:
Name Type Description
message string String representation of message received.

onOpen()

Event handler called once the connection has been opened. Attempting to send messages before this handler is called will result in an error.

send(message)

Sends a message to the web socket endpoint.
Parameters:
Name Type Description
message object Message to be sent. If not a string it converted using JSON.stringify.

setUrl(url)

Updates the underlying URL for this model.
Parameters:
Name Type Description
url string URL for model.