socket.io:client.on响应其他客户的消息

发布于 2025-01-23 01:43:12 字数 901 浏览 3 评论 0原文

因此,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2025-01-30 01:43:12

我设法解决了这个问题,有一个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.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文