Client Molecule

Add a client web socket.

Summary
Client Molecule
Preference Dialog
CommentsWeb socket requires a script molecule to receive socket events.
Properties
typestring, ( r )
pref.namestring, ( r )
pref.scriptNamestring, ( r, w )
bufferedAmountint, ( r )
protocolstring, ( r )
readyStateint, ( r )
urlstring, ( r )
binaryTypestring, ( r )
Functions
attach
detach
delete
rename
clone
serialize
connect
send
read
close

Preference Dialog

Comments

Web socket requires a script molecule to receive socket events.  The events are passed to the script as input parameter params which has the following structure,

{ type: 'open' }
{ type: 'message' }
{ type: 'close', code: closeCode, reason: theReason, wasClean: true  }
{ type: 'error'}

When an error occurs during connection, the script molecule first recieves an error event followed by a close event.  The close event provides further details about the error.  See this page for more information on close event’s attributes and a full listing of close codes.

On receiving message event, the implementer can use read function to read available data from receive buffer.

Properties

type

string, ( r )

Type of this molecule, client.

pref.name

string, ( r )

The scriptable name for this molecule.  This name can be set using rename function dynamically.

pref.scriptName

string, ( r, w )

Specifies the name of the script molecule to receive socket events.

bufferedAmount

int, ( r )

Returns the number of bytes that have been queued but not yet transmitted to the server.

protocol

string, ( r )

Returns the subprotocol selected by the server, if any.

readyState

int, ( r )

The readyState attribute represents the state of the connection.  It can have the following values,

CONNECTING(0)he connection has not yet been established.
OPEN(1)The WebSocket connection is established and communication is possible.
CLOSING(2)The connection is going through the closing handshake, or the close() method has been invoked.
CLOSED(3)The connection has been closed or could not be opened.

url

string, ( r )

Returns the result of resolving the URL that was passed to the web socket connect.

binaryType

string, ( r )

Returns the type of binary data being received, can be blob or arraybuffer.

Functions

attach

attach(droplet);

Attach this molecule to the given droplet.

Parameters

droplet(object) a droplet.

Returns

N/A

detach

detach();

Remove this molecule from its droplet.

Parameters

N/A

Returns

N/A

delete

delete();

Delete this molecule.

Parameters

N/A

Returns

N/A

rename

rename(name);

Rename this molecule.

Parameters

name(string) a new name.

Returns

N/A

clone

clone();

Create a clone of this molecule.

Parameters

N/A

Returns

(object) a molecule.

serialize

serialize();

Serialize this molecule into its JSON representation.

Parameters

N/A

Returns

(string) JSON representation of a molecule.

connect

connect(ws_url, protocols);

Connect to server at ws_url and optionally request server to select a sub-protocols to communicate.

Parameters

ws_url(string) a valid web socket server address.  (e.g. ws://game.example.com:12010/updates’.)
protocols(mixed) Optaional.  Can be either a string or an array of strings.  If it is a string, it is equivalent to an array consisting of just that string; if it is omitted, it is equivalent to the empty array.  Each string in the array is a subprotocol name.  The connection will only be established if the server reports that it has selected one of these subprotocols.

Returns

(object) a web socket.

send

send(data);

Send data to server.

Parameters

data(mixed) data can be a string, object, blob, or arraybuffer.  If it is an object, it is stringified to string before send.

Returns

N/A

read

read();

Read data available in the receive buffer.

Parameters

N/A

Returns

(mixed)

close

close();

Close web socket and clear receive buffer.

Parameters

N/A

Returns

N/A

Close