Skip to content

Instantly share code, notes, and snippets.

@kurtraschke
Created September 15, 2011 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtraschke/1220609 to your computer and use it in GitHub Desktop.
Save kurtraschke/1220609 to your computer and use it in GitHub Desktop.
Demonstration of polling disconnect in socket.io
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var connectTime;
var socket = io.connect('/', {transports: ['websocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']});
socket.on('connect', function(){
console.log('connected');
connectTime = new Date().getTime();
console.log(connectTime);
});
socket.on('disconnect', function(){
console.log('disconnected');
var disconnectTime = new Date().getTime();
console.log(disconnectTime);
console.log(disconnectTime-connectTime);
});
</script>
</head>
<body>
</body>
</html>
var httpProxy = require('http-proxy');
var express = require('express');
var io = require('socket.io');
httpProxy.createServer(8000, 'localhost').listen(9000);
var app = express.createServer();
app.configure(function() {
app.use(express.static(__dirname));
});
io = io.listen(app);
//uncomment to force jsonp-polling
//io.configure(function(){io.set('transports', ['jsonp-polling']);});
app.listen(8000);
@kurtraschke
Copy link
Author

This test mimics conditions on Joyent's no.de, where the use of node-http-proxy prevents WebSockets from working (due to http-party/node-http-proxy#97). Fallback to xhr-multipart doesn't work either; it's not clear if socket.io still supports xhr-multipart, but even if it does, node-http-proxy can't proxy it: http-party/node-http-proxy#70.

As a result, socket.io falls back to xhr-polling; as the test demonstrates, the connection reliably fails after 135 seconds. Switching to jsonp-polling gives the same results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment