【发布时间】:2019-04-29 00:29:32
【问题描述】:
每当有新用户加入房间时,我都会尝试使用“socket.io”和“node js”在房间中广播消息。这是我卡住的相关代码。(目前正在发生的事情是:
第 1 步:一位用户创建了群组(聊天室中显示消息“您创建了房间并加入了它”)
第 2 步:其他用户加入群组(消息在加入的用户上显示“已加入群组”,但在创建群组的其他用户上不显示)
尝试将用户加入群组的消息作为其在“聊天室”上的第一条消息“发出”,但并未发出该消息。
试过io.on(data.room).emit("group",...
socket.to(data.room).broadcast.emit(....
socket.broadcast.to(data.room).emit(....
服务器端 ->app.js
io.on('connection', (socket) => {
socket.on("joinGroup",(data)=>{
console.log("room clicked server",data)
// socket.broadcast.to(data.room).emit("jgroup",{username : data.username})//not emitting to jgroup, which will show that this current user joined the group
socket.to(data.room).broadcast.emit("jgroup",{username : data.username})
// io.in(data.room).emit("jgroup",{username : data.username})
//console.log(data.room)
// console.log(data.username)
console.log(io.sockets.name)
})
});
*客户端 ->chat.js
$(function () {
//make connection
console.log("hello")
var socket = io()
socket.on("jgroup",(data)=>{
console.log("11111111111111",data.username)
feedback.html('');
message.val('');
chatroom.append("<p class='message' style='font-family:courier;'>" + data.username + " has joined the group. </p>")
})
});
在此先感谢。任何帮助将不胜感激。询问是否不理解我的问题,尝试以简单明了的方式提出我的问题。
【问题讨论】: