socket.io断开后自动重连上,收到的消息就重复了,断几次就会收到多少重复消息,怎么回事?为何不能自动销毁掉。 该如何解决
8 回复
已经搞定 原因: 错误写法: var socket = io.connect(); socket.on('connect’, function () { socket.on('message’, function(message) { }); socket.on('disconnect’, function() { }); }); 正确写法: var socket = io.connect(); socket.on('connect’, function () { }); socket.on('message’, function(message) { }); socket.on('disconnect’, function() { });
我勒个去,找到问题了 因为之前不知道在哪里看的,加了下面这个代码 socketio.socket.on('error’, function(){ socketio.socket.connect(); });
socket.on('connect', function () {
try {
console.log('connect success');
} catch (e) {
}
var curTime = new Date().getTime();
Ws.reConnNum = 1;
if (Ws.lastTime && (curTime - Ws.lastTime <= 5000)) {
Ws.lastTime = curTime;
return;
}
Ws.lastTime = curTime;
socket.emit('init', {id:speek.selfid, nation:speek.selfgroup});
});
socket.on('message', function (message, callback) {
try {
console.log('message');
} catch (e) {
}
});
socket.on('disconnect', function () {
try {
console.log('disconnect');
} catch (e) {
}
});
socket.on('connect_failed', function () {
try {
console.log('connect_failed');
} catch (e) {
}
});
socket.on('error', function () {
try {
console.log('error');
} catch (e) {
}
});
socket.on('reconnect_failed', function () {
try {
console.log('reconnect_failed');
} catch (e) {
}
});
socket.on('reconnect', function () {
try {
console.log('reconnect');
} catch (e) {
}
});
socket.on('reconnecting', function () {
if (Ws.reConnNum == 3) {
setTimeout(function () {
if (Ws.reConnNum == 4) {
Ws.showDisWin();
}
}, 5000);
}
Ws.reConnNum++;
try {
console.log('reconnecting');
} catch (e) {
}
});
我也遇到楼主的问题了,不知道我这样写是否有问题???