Skip to content

Instantly share code, notes, and snippets.

@vargon
Created May 4, 2012 14:42
Show Gist options
  • Save vargon/2595194 to your computer and use it in GitHub Desktop.
Save vargon/2595194 to your computer and use it in GitHub Desktop.
Sample WebSocket client in JavaScript
var webSocketClient;
function connectToWebSocketServer() {
webSocketClient = new WebSocket('ws://localhost:1111/some/resource/');
webSocketClient.onopen = function() { alert('connection opened'); };
webSocketClient.onerror = function() { alert('connection error'); };
webSocketClient.onclose = function() { alert('connection closed'); };
webSocketClient.onmessage = function(msg) { alert('msg: '+msg.data); };
}
connectToWebSocketServer();
.
.
.
if(webSocketClient && webSocketClient.readyState == 1) {
webSocketClient.send("Hello world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment