2017-04-17 110 views
0

我正在開發一個使用android的聊天應用程序。我的客戶端部分是android。當我做私人聊天不起作用。我想發送到abc1。該消息僅在abc1上顯示。我的代碼如下所示。socket io私人聊天不起作用

客戶

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沒有顯示任何東西。怎麼可能?請幫幫我?

回答

0

我得到了答案。

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

在服務器端使用io.to(userid)而不是socket.to(to.userId)。它適合我。