我打开10个窗口。我连接了5对客户端。我将所有窗口1关闭1.只有每一次断开都会被触发,并且每次断开它们都会点火。
什么可能导致这个?
var fs = require( 'fs' );
var express = require('express');
var app = express();
var servIO = require('https').createServer({
key: fs.readFileSync('privkey.pem'),
cert: fs.readFileSync('fullchain.pem')
},app);
var serv = require('http').Server(app);
app.get('//',function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(8001);
servIO.listen(8002);
console.log("Server started.");
var SOCKET_LIST = {};
var io = require('socket.io')(servIO,{});
io.sockets.on('connection', function(socket){
socket.id = uniqueNumber();
socket.x = 0;
socket.y = 0;
socket.opponent = "";
socket.number = "" + Math.floor(10 * uniqueNumber());
SOCKET_LIST[socket.id] = socket;
for(var ii in SOCKET_LIST){
var sk = SOCKET_LIST[ii];
if(sk.opponent == "" && sk.id !== socket.id)
{
var joinedopponant = true;
SOCKET_LIST[sk.id].opponent = socket.id;
SOCKET_LIST[socket.id].opponent = sk.id;
console.log('user ' + SOCKET_LIST[sk.id].opponent + ' joined user ' + SOCKET_LIST[socket.id].opponent);
var socket = SOCKET_LIST[sk.id];
socket.emit('UserJoinedYou', socket.id);
break;
}
}
if(!joinedopponant)
{
var socket = SOCKET_LIST[socket.id];
var pack = [];
pack.push({
waiting:true
});
socket.emit('waiting', pack);
console.log('user ' + socket.id + ' waiting');
}
socket.on('disconnect',function()
{
console.log(socket.id + ' has left');
delete SOCKET_LIST[socket.id];
/*
var opponent = SOCKET_LIST[socket.id].opponent;
var socket = SOCKET_LIST[opponent];
socket.emit('UserDisConnect', 'User Left');
SOCKET_LIST[opponent].opponent = "";
console.log('user ' + opponentp + ' waiting');
*/
});
function uniqueNumber() {
var date = Date.now();
// If created at same millisecond as previous
if (date <= uniqueNumber.previous) {
date = ++uniqueNumber.previous;
} else {
uniqueNumber.previous = date;
}
return date;
}
uniqueNumber.previous = 0;
});
控制台输出是:
经过一些调试后,似乎如果这个代码块在那里它会发生,如果不是那么它就不会发生。所以原因就在这里
for(var ii in SOCKET_LIST){
var sk = SOCKET_LIST[ii];
if(sk.opponent == "" && sk.id !== socket.id)
{
var joinedopponant = true;
SOCKET_LIST[sk.id].opponent = socket.id;
SOCKET_LIST[socket.id].opponent = sk.id;
console.log(sk.id + ' - ' + socket.id);
console.log(SOCKET_LIST[sk.id].opponent + ' - ' + SOCKET_LIST[socket.id].opponent);
console.log('user ' + SOCKET_LIST[sk.id].opponent + ' joined user ' + SOCKET_LIST[socket.id].opponent);
var socket = SOCKET_LIST[sk.id];
socket.emit('UserJoinedYou', socket.id);
}
}
对我来说,为什么这段代码在断开连接时会产生任何影响是没有意义的?
这是导致问题的一条线。
SOCKET_LIST[socket.id].opponent = sk.id;
如果我评论此行,则断开连接每次都会正确触发。如果我取消对此行的评论,那么它就会执行我上面描述的内容。
答案 0 :(得分:0)
将var socket
更改为导致问题的代码中的其他名称。
它与
的套接字参数冲突io.on('connect', function(socket) ...)
这是因为变量提升类似于:
io.on('connect', function(socket) {
var socket;
// can't access the desired socket parameter here
});
或者,您也可以使用let socket
代替var socket
,因此它可以使用块范围并且不会干扰。
答案 1 :(得分:0)
我改变了
var socket = SOCKET_LIST[sk.id];
变量为;
var skup = SOCKET_LIST[sk.id];