socket.io:client.on响应其他客户的消息
因此,我在socket.io
上遇到问题:
因此,在服务器中,我将消息发送给客户端:
请注意,下面的摘要只是简化了我的内容m执行
import { Server, Socket } from 'socket.io';
const app = express();
const http = require('http');
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: '*' } });
io.on('connection', socket => {
socket.on('message', (data) => {
// ... some computation here
socket.emit('other message', computeddata); // Should only send to the
sender
});
});
,然后在客户端上接收到它:
socket.on('connect', () => {
socket.emit('message');
});
socket.on('other message', (data) => {
console.log(data);
});
当服务器向套接字发出消息时,它只能发送到特定的套接字吗? 好吧,所有连接的插座上都拿起。
我该如何做到这一点,以使消息只发送给发件人?
我对socket.io有些新, 提前致谢!
so i'm having a problem with Socket.io
:
So in the server i'm sending a message to the client like that:
Note that the snippets below are just simplifications of what i'm doing
import { Server, Socket } from 'socket.io';
const app = express();
const http = require('http');
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: '*' } });
io.on('connection', socket => {
socket.on('message', (data) => {
// ... some computation here
socket.emit('other message', computeddata); // Should only send to the
sender
});
});
And on the client im receiving it like that:
socket.on('connect', () => {
socket.emit('message');
});
socket.on('other message', (data) => {
console.log(data);
});
When the server emits the message to the socket, it should only send to the specific socket right?
Well, ALL the connected sockets pick up on it.
How can I make it so that the message only sends to the sender?
I'm a bit new to socket.io,
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法解决了这个问题,有一个
io.emit
行,我以某种方式错过了3次,这破坏了其余的发射。哦,上帝.....
i managed to fix the issue, there was a
io.emit
line that I somehow missed 3 times, that ruined the rest of the emits.Oh god.....