Created
May 4, 2012 14:42
-
-
Save vargon/2595194 to your computer and use it in GitHub Desktop.
Sample WebSocket client in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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