最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

发件人如何使用socket.io看到自己的消息?

运维笔记admin16浏览0评论

发件人如何使用socket.io看到自己的消息?

发件人如何使用socket.io看到自己的消息?

我正在使用socket.io,我想通过它的socket.id向特定用户发送消息,并且发送者也会看到自己的消息。我得到了socket.id并发送消息:socket.to(msg.id).emit('chat message', msg.message);,其中msg.id是收件人的socket.id

我发现我无法得到socket.to(<socketid>).emit('hey', 'I just met you');的消息,其中<socketid>是我自己的。

这是我从服务器获取消息并将其发送给特定用户的部分:

io.on('connection', function(socket){
socket.on('chat message', function(msg){
    socket.to(msg.id).emit('chat message', msg.message);
  });
});

如何让发件人也能看到它的消息,而不只是看到来自其他人的消息?

希望你能帮助我。

回答如下:

请检查一下......

我希望它对你有所帮助

io.on('connection', function (socket) { 
  socket.on('chat message', function (msg) {   
    socket.broadcast.emit('chat message', {
      userid: socket.id,
      message: msg
    });
  });
发布评论

评论列表(0)

  1. 暂无评论