简体   繁体   English

TypeError:断开连接时无法读取未定义的属性“id”

[英]TypeError: Cannot read property 'id' of undefined on disconnect

Probably it's a stupid error but i dont know how to solve it.可能这是一个愚蠢的错误,但我不知道如何解决它。 This is my server side code.这是我的服务器端代码。

// Dependencies
var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');

var app = express();
var server = http.Server(app);
var io = socketIO(server);

app.set('port', 4040);
app.use('/client', express.static(__dirname + '/client'));

app.get('/', function(request, response) {
    response.sendFile(path.join(__dirname, '/client/index.html'));
});

// Starts the server.
server.listen(4040, function() {
    console.log('Starting server on port 4040');
});

// Add the WebSocket handlers
var SOCKET_LIST = {};
var playerNumber = 0;
var started = false;

io.on('connection', function(socket) {
    socket.id = Math.random();
    SOCKET_LIST[socket.id] = socket;

    console.log(socket.id);
    console.log(SOCKET_LIST);
    console.log(Object.keys(SOCKET_LIST));

    socket.on('disconnect', function() {    
        delete SOCKET_LIST[socket.id];
    });
});

When i activate server with node and a client connects it correctly print the three console.log() containing: socket.id, SOCKET_LIST and list of keys in SOCKET_LIST with the key corrisponding to socket.id.当我用节点激活服务器并且客户端正确连接它时,打印三个console.log()包含:socket.id,SOCKET_LIST和SOCKET_LIST中的键列表,键对应于socket.id。

The problem is that on disconnect i receive the error:问题是在断开连接时我收到错误:

delete SOCKET_LIST[socket.id];删除 SOCKET_LIST[socket.id]; ^ ^

TypeError: Cannot read property 'id' of undefined at Socket. TypeError:无法读取 Socket 未定义的属性“id”。 (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\app.js:82:31) at Socket.emit (events.js:315:20) at Socket.emit >C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\socket.io\lib\socket.js:142:10) at Socket.onclose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\socket.io\lib\socket.js:454:8) at Client.onclose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\socket.io\lib\client.js:254:24) at Socket.emit (events.js:327:22) at Socket.onClose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\engine.io\lib\socket.js:318:10)> at Object.onceWrapper (events.js:421:28) at WebSocket.emit (events.js:315:20) at WebSocket.Transport.onClose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\engine.io\lib\transport.js:127:8) (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\app.js:82:31) 在 Socket.emit (events.js:315:20) 在 Socket.emit >C:\Users\utente\Desktop\SistemiOrientatiAdInternet \Progetto\node_modules\socket.io\lib\socket.js:142:10) 在 Socket.onclose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\socket.io\lib\socket.js:454 :8) 在 Client.onclose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\socket.io\lib\client.js:254:24) 在 Socket.emit (events.js:327:22)在 Socket.onClose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\engine.io\lib\socket.js:318:10)> 在 Object.onceWrapper (events.js:421:28) 在 Z45EDCADD1B96407DA39 .emit (events.js:315:20) 在 WebSocket.Transport.onClose (C:\Users\utente\Desktop\SistemiOrientatiAdInternet\Progetto\node_modules\engine.io\lib\transport.js:127:8)

Any suggestions?有什么建议么?

The reason is once your socket has disconnected, it no longer exists and you can't access its id anymore.原因是一旦您的套接字断开连接,它就不再存在并且您无法再访问它的 ID。 You can try saving the id in a variable so you can remove it from your dictionary later.您可以尝试将 id 保存在变量中,以便稍后将其从字典中删除。

socket.id = Math.random();
let socketId = socket.id
SOCKET_LIST[socketId] = socket;

...

socket.on('disconnect', function() {    
  delete SOCKET_LIST[socketId];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM