Skip to content

Instantly share code, notes, and snippets.

@incepttechnologies
Created October 18, 2013 18:58
Show Gist options
  • Save incepttechnologies/7046388 to your computer and use it in GitHub Desktop.
Save incepttechnologies/7046388 to your computer and use it in GitHub Desktop.
WebSocket Client Side using javascript
<script type="text/javascript">
var host = "ws://localhost:8080/JSFWebSockets/notification";
var wSocket = new WebSocket(host);
var browserSupport = ("WebSocket" in window) ? true : false;
// called body onLoad()
function initializeReception()
{
if (browserSupport)
{
wSocket.onopen = function()
{
//alert(" Web Socket is connected, sending data");
wSocket.send("ping");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket is NOT supported by your Browser!");
}
}
// called when a message is received
wSocket.onmessage = function(event)
{
var received_msg = event.data;
document.getElementById('headerForm:serverMsg').value = received_msg;
};
// called when socket closes
wSocket.onclose = function()
{
// websocket is closed.
//alert("Connection is closed...");
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment