尝试将套接字连接到房间时遇到问题。房间的名称是socket.id,并在连接时分配。但是,发生这种情况时,它不会加入房间。可能是什么错误?
客户代码:
var socket = io();
socket.on("connect", function() {
socket.emit("joinRoom", socket.id);
});
socket.on("agentMessage", function(msg) {
$('#messages').append("<div class='agent-messages'><p>" + msg + "</p></div>");
});
服务器代码:
io.on("connection", (socket) => {
let roomID;
socket.on("joinRoom", (socket_id) => {
roomID = socket_id;
socket.join(roomID);
socket.to(roomID).emit("agentMessage", "Welcome!");
});
socket.on("disconnect", () => {
socket.leave(socket_id);
});
});