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
- autoReconnect
- SC.WebSocket.CLOSED
- SC.WebSocket.CLOSING
- SC.WebSocket.CONNECTING
- delegate
- isAuth
- isConnected
- isJSON
- SC.WebSocket.OPEN
- reconnectInterval
- server
- Fields borrowed from SC.Object:
- concatenatedProperties, isDestroyed, isObject, nextProperty, object, property, target, toInvalidate
- Fields borrowed from SC.Observable:
- isObservable
Instance Methods
Field Detail
autoReconnect SC.WebSocketDelegate- Default value:
- true
- Default value:
- 3
- Default value:
- 2
- Default value:
- 0
- Default value:
- null
- See:
- SC.WebSocketDelegate
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
- Default value:
- true
- Default value:
- 1
- Default value:
- null
- Default value:
- null
Instance Method Detail
- 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.
- Returns:
- SC.WebSocket
- The SC.WebSocket object.
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 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