如何在 socket.io 中的断开连接事件中获取断开连接的客户端的套接字 ID

How to get the socket id of a disconnected client on the disconnect event in socket.io

我已经开始为 othello 开发一个 Web 应用程序....在其中我使用 node.js 和 socket.io 来处理服务器端代码。它在云端运行一个 server.js 文件。

此文件处理一些主要的客户端事件,其中之一是断开连接事件。

这是断开连接事件的代码:

io.sockets.on('connection', function(socket) {
    log('Client connection by '+socket.id);
    function log(){
        var array = ['*** Server log Message'];
        for(var i=0; i< arguments.length; i++) {
            array.push(arguments[i]);
            console.log(arguments[i]);
        }
        socket.emit('log', array);
        socket.broadcast.emit('log', array);
    }
    /* disconnect command */
    socket.on('disconnect', function(socket) {
    log(socket);
    log('Client disconnected '+ JSON.stringify(players[socket.id]));
    if('undefined' !== typeof players[socket.id] && players[socket.id]) {
        var username = players[socket.id].username;
        var room = players[socket.id].room;
        var payload = {
            username: username,
            socket_id: socket.id
        };
        delete players[socket.id];
        io.in(room).emit('player_disconnected', payload);
    }
    });
});

此断开连接命令应将断开连接的播放器通知所有其他客户端并删除有关它的数据。

保存活跃玩家临时数据的代码如下:

/* join_room command */
socket.on('join_room', function(payload) {
    log('\'join_room\' command '+ JSON.stringify(payload));
    if(('undefined' === typeof payload) || !payload) {
        var error_message = 'join_room had no payload, command aborted';
        log(error_message);
        socket.emit('join_room_response', {
            result: 'fail',
            message: error_message
        });
        return;
    }
    var room = payload.room;
    if(('undefined' === typeof room) || !room) {
        var error_message = 'join_room didn\'t specify a room, command aborted';
        log(error_message);
        socket.emit('join_room_response', {
            result: 'fail',
            message: error_message
        });
        return;
    }
    var username = payload.username;
    if(('undefined' === typeof username) || !username) {
        var error_message = 'join_room didn\'t specify a username, command aborted';
        log(error_message);
        socket.emit('join_room_response', {
            result: 'fail',
            message: error_message
        });
        return;
    }

    /* store information about new player */
    players[socket.id] = {};
    players[socket.id].username = username;
    players[socket.id].room = room;
    log(players);

    socket.join(room);

    var roomObject = io.sockets.adapter.rooms[room];

    /* notify others about new player */


    var sumCleints = roomObject.length;
    var data = {
        result: 'success',
        room: room,
        username: username,
        socket_id: socket.id,
        membership: sumCleints
    };
    io.in(room).emit('join_room_response', data);

    for(var socket_in_room in roomObject.sockets) {
        var data = {
            result: 'success',
            room: room,
            username: players[socket_in_room].username,
            socket_id: socket_in_room,
            membership: sumCleints
        };
        socket.emit('join_room_response', data);
    }
    log('join_room success');

    log('Room: '+ room + ' was just joined by '+ username)
});

但问题是事实并非如此。当我记录 socket.id 时,它 returns 未定义,我不知道为什么......当我记录套接字本身时,它说:传输关闭。

我的问题是如何获取刚刚断开连接的玩家的套接字ID。

这是处理 player_disconnected 事件的客户端代码:

/* when someone leaves a room */
socket.on('player_disconnected', function(payload) {
    if (payload.result == 'fail') {
        alert(payload.message);
        return;
    }

    if(payload.socket_id == socket_id) {
        return;
    }

    /* Delete all rows for new players that leave */
    var dom_elements = $('.socket_'+payload.socket_id);

    if(dom_elements.length != 0) {
        dom_elements.slideUp(1000);
    }

    var newHTML = '<p>'+payload.username+' has left the lobby</p>';
    var newNode = $(newHTML);
    newNode.hide();
    $('#messages').append(newNode);
    newNode.slideDown(1000);
});

如果有人能解决问题,请告诉我,请告诉我断开连接事件和其他事件的实际工作原理以及它们的参数是什么,因为我在中找不到任何有用的信息文档...提前致谢。

断开连接时,您将使用不同的回调参数覆盖 socket 变量。试试这个:

/* disconnect command */
socket.on('disconnect', function() {
  console.log(socket.id);
});