Skip to content

Instantly share code, notes, and snippets.

@ktkaushik
Created November 23, 2012 19:25
Show Gist options
  • Select an option

  • Save ktkaushik/4136930 to your computer and use it in GitHub Desktop.

Select an option

Save ktkaushik/4136930 to your computer and use it in GitHub Desktop.
check if browser is WebSocket compatible
$(document).ready(function() {
if( typeof(WebSocket) != "function" ) {
// Display error message here !
}
});
// Check if the user's browser is WebSocket(WS) compatible
// http://bit.ly/SiWEDV for more info
function isBrowserWSCompatible () {
var isCompatible = false;
if ("WebSocket" in window) {
isCompatible = true;
};
return isCompatible;
};
if ("WebSocket" in window) {
// different ways to create WebSocket using Faye though
var ws = new WebSocket("ws://example.com/service");
ws.onopen = function() {
// Web Socket is connected. You can send data by send() method.
ws.send("message to send"); ....
};
ws.onmessage = function (evt) { var received_msg = evt.data; ... };
ws.onclose = function() { // websocket is closed. };
} else {
// the browser doesn't support WebSocket.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment