Javascript SocketIO离开房间功能不工作

Javascript SocketIO离开房间功能不工作,javascript,node.js,socket.io,logic,Javascript,Node.js,Socket.io,Logic,我正在用SocketIO开发多聊天室。看来加入房间的功能很好,但离开房间却不起作用 我用三种不同的ID测试了加入和离开函数测试 每个房间链接都有id值,如 [客户端代码] var socket = io.connect('IPADDRESS'); let current; function joinChat() { let joinedRoomName = window.event.target.id; // Get clicked id (ROOM NAME) $('.ms

我正在用SocketIO开发多聊天室。看来加入房间的功能很好,但离开房间却不起作用

我用三种不同的ID测试了加入和离开函数测试

每个房间链接都有id值,如

[客户端代码]

var socket = io.connect('IPADDRESS');
let current;

function joinChat() {
    let joinedRoomName = window.event.target.id; // Get clicked id (ROOM NAME)

    $('.msg_history').empty(); // to Remove Previous Chats
    socket.emit('JoinRoom', {
        joinedRoomName,
        leave: current,
    });
    current = joinedRoomName;
    console.log(`CURRENT ROOM : ${current}`);
    console.log(`NewJoined ROOM ${joinedRoomName}`)

    $('#chat').submit(function () {
        //submit only if it's not empty
        if ($('#message').val() != "") {
            var msg = $('#message').val();
            socket.emit('say', {
                msg: msg,
                userId: userId,
                loginedId: loginedId,
                joinedRoomName: joinedRoomName
            });
            //say event means someone transmitted chat
            $('#message').val('');
        }
        return false;
    });
}

$(function () {
    socket.on('mySaying', function (data) {
        if (data.userId != userId) {
            $('.msg_history').append(`<div class="incoming_msg"><div class="incoming_msg_img"><img src="https://ptetutorials.com/images/user-profile.png" alt="sunil"></div><div class="received_msg"><div class="received_withd_msg"><p>${data.msg}</p><span class="time_date"> 11:01 AM    |    June 9</span></div></div></div>`);
        } else {

            $('.msg_history').append(`<div class="outgoing_msg"><div class="sent_msg"><p>${data.msg}</p><span class="time_date"> 11:01 AM    |    June 9</span></div></div>`);
        }
    });

// Socket IO 
io.on('connection', function (socket) {
  // Join Room
  socket.on('JoinRoom', function (data) {
    socket.leave(`${data.leave}`);
    console.log(`Leave ROOM : ${data.leave}`)
    socket.join(`${data.joinedRoomName}`);
    console.log(`NEW JOIN IN ${data.joinedRoomName}`)
  })

  // Send Message
  socket.on('say', function (data) {
    console.log(`${data.userId} : ${data.msg}`);
    //chat message to the others
    //mySaying to the speaker
    io.sockets.to(`${data.joinedRoomName}`).emit('mySaying', data);
    console.log(`Message Send to : ${data.joinedRoomName}`)
    // console.log(`Message Content : ${data.userId} : ${data.message}`);
    db.query(`INSERT INTO chatData (roomName, chatSender, chatMessage) VALUES (?,?,?)`, [data.joinedRoomName, data.userId, data.msg])
  });
})
用户1与“房间1”聊天->有效! 用户2与“房间1”聊天->有效! 因此,用户1和用户2可以互相聊天

但如果用户2移动到“房间2”,则“房间1”应该离开,但用户2似乎仍与“房间1”连接。所以用户2聊天时,它仍然会去1号房间

然而,真的很奇怪,如果用户1发送消息,在用户2的聊天页面中就不能得到任何消息。但如果用户3向用户2发送消息,则获取消息,但无法发送给用户3


我认为这是聊天逻辑问题实际上,这是一个非常简单的问题。我只是在外部设置joinedRoomName变量并移动提交函数

var socket = io.connect('IPADDRESS');
let joinedRoomName, current, others;

/* Click Each Room list Function */
function joinChat() {
    joinedRoomName = window.event.target.id; // Get clicked id (ROOM NAME)
    others = document.getElementById(joinedRoomName).innerHTML; // Talk with this person
    $('.msg_history').empty(); // to Remove Previous Chats
    socket.emit('JoinRoom', {
        joinedRoomName,
        leave: current,
    });
    current = joinedRoomName;
    console.log(`CURRENT ROOM : ${current}`);
    console.log(`NewJoined ROOM ${joinedRoomName}`)
}
请看我的回答: