Class: SC.WebSocket


Extends SC.DelegateSupport, SC.Object.

Implements SproutCore run loop aware event handling for WebSocket. Using SC.WebSocket ensures that the run loop runs on each WebSocket event and provides a useful skeleton for handling WebSocket events.

Example Usage:

// Create a WebSocket connection.
var ws = SC.WebSocket.create({
  server: 'ws://server',
});

// Assign target and methods for events.
ws.notify('onopen', this, 'wsOpened');
ws.notify('onmessage', this, 'wsReceivedMessage');
ws.notify('onclose', this, 'wsClosed');
ws.connect();

// Send a message through the WebSocket.
ws.send('hello server');

Defined in: websocket.js

Since:
SproutCore 1.11

Field Summary

Instance Methods

Field Detail

autoReconnect SC.WebSocketDelegate
Whether to attempt to reconnect automatically if the connection is closed or not.
Default value:
true
SC.WebSocket.CLOSED Number
The connection is closed or couldn't be opened.
Default value:
3
SC.WebSocket.CLOSING Number
The connection is in the process of closing.
Default value:
2
SC.WebSocket.CONNECTING Number
The connection is not yet open.
Default value:
0
delegate SC.WebSocketDelegate
A WebSocket delegate.
Default value:
null
See:
SC.WebSocketDelegate
isAuth Boolean

In order to handle authentication, set isAuth to false in the webSocketDidOpen delegate method just after sending a request to authenticate the connection. This way, any futher messages will be put in the queue until the server tells you that the connection is authenticated. Once it is, you should set isAuth to true to resume the queue.

If you don't need authentication, leave isAuth as null.

Default value:
null
isConnected Boolean
Whether the connection is open or not.
isJSON Boolean
Processes the messages as JSON if possible.
Default value:
true
SC.WebSocket.OPEN Number
The connection is open and ready to communicate.
Default value:
1
reconnectInterval SC.WebSocketDelegate
The interval in milliseconds to wait before trying to reconnect.
Default value:
null
server String
The URL of the WebSocket server.
Default value:
null

Instance Method Detail

close(code, reason)
Close the connection.
Parameters:
code Number
A numeric value indicating the status code explaining why the connection is being closed. If this parameter is not specified, a default value of 1000 (indicating a normal "transaction complete" closure) is assumed.
reason String
A human-readable string explaining why the connection is closing. This string must be no longer than 123 bytes of UTF-8 text (not characters).
Returns:
SC.WebSocket
The SC.WebSocket object.
connect()
Open the WebSocket connection.
Returns:
SC.WebSocket
The SC.WebSocket object.
notify(target, target, action)

Configures a callback to execute when an event happens. You must pass at least a target and method to this and optionally an event name.

You may also pass additional arguments which will then be passed along to your callback.

Example:

var websocket = SC.WebSocket.create({ server: 'ws://server' }).connect();

webSocket.notify('onopen', this, 'wsWasOpened');
webSocket.notify('onmessage', this, 'wsReceivedMessage');
webSocket.notify('onclose', this, 'wsWasClose');
webSocket.notify('onerror', this, 'wsDidError');

Callback Format

Your notification callback should expect to receive the WebSocket object as the first parameter and the event or message; plus any additional parameters that you pass. If your callback handles the notification and to prevent further handling, it should return YES.

Parameters:
target String
String Event name.
target Object
The target object for the callback action.
action String|Function
The method name or function to call on the target.
Returns:
SC.WebSocket
The SC.WebSocket object.
send(message)

Send the message on the WebSocket. If the connection is not yet open or authenticated (as necessary), the message will be put in the queue.

If isJSON is true (the default for SC.WebSocket), the message will be stringified JSON.

Parameters:
message String|Object
The message to send.
Returns:
SC.WebSocket
Documentation generated by JsDoc Toolkit 2.4.0 on Wed Apr 08 2015 10:02:21 GMT-0600 (CST)