gpt4 book ai didi

sockets - 在事件之间将属性附加到套接字

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

我发现我可以将名称等附加到socket.io套接字。

这很好。

但是我想在另一个事件上检索相同的数据。

这应该显示得很清楚:

socket.on('studentJoinRoom', function answerStudentJoin(msg) {

var userName = msg.userName;
var roomId = msg.roomId;

console.log('user ' + userName + ' joining room ' + roomId);
socket.userName = userName;

if (socket.room !== roomId) { // only join once
socket.room = roomId;
testRooms.joinClient(userName, roomId);
socket.join(roomId); // join socket room
socket.emit('studentJoinConfirmed');
socket.broadcast.to(testRooms.getTeacherId(roomId)).emit('newStudentJoined', {student: userName});
}
console.dir(testRooms.getRooms());
});


socket.on('disconnect', function(socket) {
console.log(socket.userName + ' has left the room ' + socket.room);
})

我想从 on('studentJoinRoom'内的 on('disconnect访问数据。我怎样才能做到这一点?

控制台输出:
user test joining room ijzvqmau
{ ijzvqmau:
{ Id: 'ijzvqmau',
teacherName: 'testteacher',
teacherId: '/#KMGUyCINopRDvf5JAAAE',
clients: [ 'test' ] } }
undefined has left the room undefined

最佳答案

您一定不要传递给回调函数'socket',因此只需将其删除即可:

socket.on('disconnect', function() {
console.log(socket.userName + ' has left the room ' + socket.room);
})

关于sockets - 在事件之间将属性附加到套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35088999/

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