Skip to content

Instantly share code, notes, and snippets.

@ichernev
Created July 3, 2012 12:10
Show Gist options
  • Save ichernev/3039375 to your computer and use it in GitHub Desktop.
Save ichernev/3039375 to your computer and use it in GitHub Desktop.
client reconnect on server disconnect
// Generated by CoffeeScript 1.3.3
var ioClientLib, ioServerLib, setupClient, setupServer;
ioServerLib = require('socket.io');
ioClientLib = require('socket.io-client');
setupServer = function() {
var ioServer;
console.log('[server] setup');
ioServer = ioServerLib.listen(typeof server !== "undefined" && server !== null ? server : 3030);
ioServer.sockets.on('connection', function(socket) {
console.log('[server] connected');
socket.join('room');
socket.on('got-it', (function() {
var cnt;
cnt = 0;
return function() {
cnt += 1;
console.log('[server] got-it event');
if (cnt === 2) {
console.log('[server] calling disconnect');
return socket.disconnect();
}
};
})());
return socket.on('disconnect', function() {
return console.log('[server] disconnect event');
});
});
return setInterval(function() {
console.log('[server] emitting msg');
return ioServer.sockets["in"]('room').emit('msg');
}, 2000);
};
setupClient = function() {
var ioClient;
console.log('[client] setup');
ioClient = ioClientLib.connect('127.0.0.1:3030', {
transports: ['xhr-polling'],
'try multiple transports': false
});
ioClient.on('connect', function() {
return console.log('[client] connected');
});
ioClient.on('msg', (function() {
var cnt;
cnt = 0;
return function() {
cnt += 1;
console.log("[client] received " + cnt + " msg");
return ioClient.emit('got-it');
};
})());
return ioClient.on('disconnect', function() {
return console.log('[client] disconnect event');
});
};
setupServer();
setTimeout(setupClient, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment