我正在尝试向特定房间广播一些信息:
服务器端
var io = require('socket.io')(server, { path: '/my/path' });
server.listen(8090, function () {
console.log("Server ON.");
});
io.on('connection', function(socket){
socket.on('room', function(room) { //set the room
socket.join(room);
socket.room = room;
});
socket.on('new message', function (data) { //receive and broadcast the msg
socket.broadcast.to(socket.room).emit('new message', data);
});
});
客户端
//initial client config
var socket = io(URL_SOCKET, {path: '/my/path'});
socket.emit('room', room_id);
socket.on('new message', function (data) {
do_something(data);
});
function on_change(data){
socket.emit('new message', data);
}
这里的问题是:
socket.broadcast.to
正在将客户端(通过调用on_change
)发送的信息广播到所有房间(所有人)。我究竟做错了什么?
最佳答案
io.in(socket.room).emit('requestRes',{success:true,STATUSCODE:2000,message:"hello"});
尝试搜索套接字备忘单。
ps:https://socket.io/docs/emit-cheatsheet/#
关于node.js - 套接字io不向特定房间广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43790685/