Use Node.js in your end devices(QQ: 796448809)
The WebSocket v13 client implementation module, which is compatible with WebSocket-Node.
Example
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
console.log('WebSocket Client Connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
}
});
function sendNumber() {
if (connection.connected) {
var number = Math.round(Math.random() * 0xFFFFFF);
connection.sendUTF(number.toString());
setTimeout(sendNumber, 1000);
}
}
sendNumber();
});
client.connect('ws://localhost:8080/', 'echo-protocol');
var WebSocketClient = require('websocket').client;
Creates a new WebSocket
client.
url
{String} the url to connect.protocol
{String} the websocket sub protocol param.'connect'
connection
{WebSocketConnection} the returned connection object.Emitted on successful (re)connection (i.e. connack rc=0).
'connectFailed'
Emitted when a connection is failed.
data {String |
Buffer} the data to send as a text frame. |
Send a text frame.
data
{Buffer} the data to send as a binary frame.Send a binary frame.
'message'
frame
{Object} the received frame object.
type
{String} utf8
or binary
.utf8Data
{String} the data when type is utf8
.binaryData
{Buffer} the data when type is binary
.'close'
Emitted when connection is close.
'error'
err
{Error} the error.Emitted when an error occurs.
This module supported the W3C WebSocket Standard API, see the following:
var W3CWebSocket = require('websocket').w3cwebsocket;
var client = new W3CWebSocket('ws://localhost:8080/', 'echo-protocol');
client.onerror = function() {
console.log('Connection Error');
};
client.onopen = function() {
console.log('WebSocket Client Connected');
function sendNumber() {
if (client.readyState === client.OPEN) {
var number = Math.round(Math.random() * 0xFFFFFF);
client.send(number.toString());
setTimeout(sendNumber, 1000);
}
}
sendNumber();
};
client.onclose = function() {
console.log('echo-protocol Client Closed');
};
client.onmessage = function(e) {
if (typeof e.data === 'string') {
console.log("Received: '" + e.data + "'");
}
};
For more detailed, please see WebSocket-Node Readme.