Skip to content

Instantly share code, notes, and snippets.

@alloyking
Created January 23, 2013 16:28
Show Gist options
  • Save alloyking/4609163 to your computer and use it in GitHub Desktop.
Save alloyking/4609163 to your computer and use it in GitHub Desktop.
websocket polling fallback
ioSocket = io.connect("http://www.mydomain:443/chatserver");
ioSocket.of('/chatserver').on('connect', function()
{
// Everything works fine
console.info("Connected to Chat-server");
}).on('connect_failed', function(reason)
{
/* Connection failed. This can have many reasons.
One possible reason is the blocking of port 443 which I used above.
In this case, the given reason-argument is just "undefined".
So if you want to have a more specific handler, you could do the following steps
only if reason == undefined. (not tested though)
*/
io.transports = ['xhr-polling']; // Limit transport to xhr-polling now. This is possible on client-side, too!
ioSocket = io.connect("http://www.mydomain:443/chatserver");
console.info("Using XHR-polling because of connection issue: "+reason);
ioSocket.of('/chatserver').on('connect', function()
{
// Now, everything works. Fallback successful!
console.info("Connected to Chat-server");
}).on('connect_failed', function(reason)
{
// Still not working..
console.error ("Connection failed: "+reason);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment