I am working on socket.io and i am new to this. I am trying to disconnect after n number of attempts to reconnect.
Server Side Code: var io = require('socket.io').listen(4000);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client side code is:
<script src="http://localhost:4000/socket.io/socket.io.js"></script>
<script>
//var max_reconnects, socket;
var max_reconnects = 5;
var socket = io.connect('http://localhost:4000',
{'reconnection delay': 100,
'max reconnection attempts': max_reconnects,
'reconnection limit' : 10});
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
socket.on("reconnecting", function(delay, attempt) {
console.log("attempting reconnect - " + attempt + "-->" + delay);
if (delay === max_reconnects) {
//return console.log("all reconnect attempts failed");
//socket.disconnect();
//socket.server.close();
socket.emit('forceDisconnect');
console.log("Limited end");
}
});
Please guide me how to terminate the reconnecting activity after number of attempts. the given code is not working properly. and reconnecting call back attempts is also undefined. Please guide me
Are you using socket.io >= 1.0 but using an example from before (there were some changes)?
Check here for the correct parameters: https://github.com/Automattic/socket.io-client#managerurlstring-optsobject
var socket = io.connect('http://localhost:4000',
{'reconnectionDelay': 100,
'reconnectionAttempts': max_reconnects});