gpt4 book ai didi

node.js - 套接字io不向特定房间广播

转载 作者:行者123 更新时间:2023-12-03 11:59:38 25 4
gpt4 key购买 nike

我正在尝试向特定房间广播一些信息:

服务器端

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/

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