Version2 /
web-socket-client module(redirected from VersionCurrent.WebSocketClient) This is version 2 of webSocketClient.
The webSocketClient module supports web socket clients. Web sockets differ from HTTP interactions by including a notion of a bidirectional connection called a "socket". It differs from a TCP socket in that the connection carries not just a byte stream, but a sequence of "messages," where each message can have an arbitrary number of bytes. It also differs from a TCP socket in that the connection is established through HTTP and is supported by most web browsers. This module defines one class, Client. To make a connection, create an instance of Client (using new) and set up listeners and/or invoke the send() function of the client to send a message. This module also provides two utility functions that return arrays of MIME types supported for sending or receiving messages. Specifying a message type facilitates conversion between the byte streams transported over the socket and JavaScript objects that are passed to send() or emitted as a 'message' event. These functions specify what types are supported by a particular implementation of this module. Implementations of this module should support at least the following types:
Module Functions
Client ClassA Client is constructed (using new) by the Client function, which takes one (optional) argument, an options object, defined below. A Client instance represents a single web socket connection to a server and can send and receive messages to and from that server. A Client object is an EventEmitter, and you can register handlers for the following events:
The functions provided by the Client class are:
The type of data sent and received can be specified with the sendType and receiveType options. In principle, any MIME type can be specified, but the host may support only a subset of MIME types. The client and the server have to agree on the type, or the data will not get through correctly. The default type for both sending and receiving is 'application/json'. For this type, the host will use JSON.stringify() to convert any JavaScript object passed to send() into a string, and for received messages, will attempt to parse the message as a JSON string. An example usage is given below: var webSocket = require('@accessors-modules/web-socket-client'); var client = new webSocket.Client({'host': 'localhost', 'port': 8080}); client.send({'foo': 'bar'}); client.on('message', function(message) { console.log('Received from web socket: ' + message); }); client.open(); The above code sends a message even before the socket is opened. The module implementation is expected to queue that message to be sent later when the socket is opened (unless the option discardMessagesBeforeOpen is given with value true). The options argument to the Client constructor is a JSON object that can contain the following properties:
See Also
Remote Resources
|