【问题标题】:socket io private chat is not working套接字 io 私人聊天不起作用
【发布时间】:2017-09-13 00:52:47
【问题描述】:

我正在使用 android 开发一个聊天应用程序。我的客户端部分是android。当我进行私人聊天时不起作用。我想发送到 abc1。该消息仅显示在 abc1 上。我的代码如下所示。

客户

mSocket.emit("send message", "msg data", "abc1");

服务器

socket.on('send message', function (message, to) {
console.log('send message');

  socket.to(to.userId).emit('new message', {
    username: socket.username,
    message: message,
    timestamp: Date.now()
  });
});

它将打印发送消息。但是 abc1 没有显示任何东西。怎么可能?请帮帮我?

【问题讨论】:

    标签: java android node.js sockets socket.io


    【解决方案1】:

    我得到了答案。

    io.to(userid).emit('new message', {
        username: socket.username,
        message: message,
        timestamp: Date.now()
      });
    

    在服务器端使用 io.to(userid) 而不是 socket.to(to.userId)。它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多