如何在跨域使用particular_room [io.to(socket.id).emit('sendData') ]发出事件
我试过这个,
server side
io.emit('sendData', data);
clientside
var socket = io('https://localhost:3000/', { transports: ['websocket'] });
socket.on('sendData', function (data) {
console.log(data);
})以上语法在跨域上很好地工作。
但是我想在跨域发出特定的空间
io.to(socket.id).emit('sendData', data)
io.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid参考链接-> Cross-domain connection in Socket.IO
请帮忙解决这个问题!
发布于 2022-10-17 11:51:47
我理解您的代码,您只需在socket.io服务器上发出初始连接。
io.on("connection", function(socket){
socket.emit('sendsocketid', data);
})
Cross-domain clientside
var socket = io('https://localhost:3000/', { transports: ['websocket'] });
var obj= [];
socket.on('sendsocketid', function (data) {
console.log(data);
// Now You Can Customize the or add the client Socket .id
var NewSocketid = {
id: socket.id
}
Obj.push(data, NewSocketid)
socket.emit("emitnewSocketId", Obj)
})
socket.on("emitnewSocketId", function(data){
consoleo.log(Obj[1].NewSocketid)
//Now You can emit the new socket id
io.to(Obj[1].NewSocketid).emit('sendData', data)
})https://stackoverflow.com/questions/74029305
复制相似问题