node-websockets
Web Socket Server and Client API
Support Protocols
Install
npm install websockets
Usage
require websockets
var websockets = ;
Server:
Server is a wrapper of http/https
server.
// http based servervar server = websockets;server; // https based servervar secure = websockets;secure;
Extended Servers such as express are also available.
// In case of 'express'var express = ; var svr = express;svr;svr; var server = websockets;server;
Client:
Client has the interfaces like html5 WebSocket.
var socket = 'wss://127.0.0.1';socket;
APIs
websockets.Server
Event: 'connect'
function (socket) {}
Emitted when client-server opening handshake has succeeded. socket
is an instance of WebSocket
.
server.broadcast(string)
Not Implemented.
Sends string
to all clients connected with server
.
server.broadcast(buffer)
Not Implemented.
Sends binary data(buffer
) to all clients connected with server
.
websockets.WebSocket
Event: 'open'
function () {}
Emitted when a client-server connection is successfully established.
Event: 'message'
function (data) {}
Emitted when the socket has received a message. The type of data
is either string
(string data) or Buffer
(binary data).
Event: 'error'
function (exception) {}
Emitted on error. exception
is an instance of Error.
Event: 'close'
function () {}
Emitted when a client-server connection has closed.
socket.send(string)
Sends string
to the other endpoint.
socket.send(buffer)
Sends binary data(buffer
) to the other endpoint.
socket.close()
Sends a connection close request to the other endpoint.
TODO
- implementation of server broadcast