WebSocket
This code shows how to communicate with server using WebSocket.
var webSocketUrl = 'ws://echo.websocket.org';
//open connection to WebSocket
var webSocket = new WebSocket(webSocketUrl);
//success callback
webSocket.onopen = function(e)
{
console.log('connection open, readyState: ' + e.target.readyState);
if(e.target.readyState == 1) {
//send message to server
webSocket.send('message');
}
};
//error callback
webSocket.onerror = function(e)
{
console.log('error: ' + e.message);
};
//receiving message callback
webSocket.onmessage = function(e)
{
console.log('server message: ' + e.data);
};