gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:17:28 26 4
gpt4 key购买 nike

我已经开始为 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时,它返回未定义,我不知道为什么......当我记录套接字本身时,它说:传输关闭。

我的问题是如何获取刚刚断开连接的玩家的套接字 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);
});

关于javascript - 如何在socket.io中的断开事件上获取断开连接的客户端的套接字ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com