我正在使用socket.io从用户(App)聊天到管理员(浏览器)。这里admin msg已经正确接收给用户,但管理员没有收到用户消息。我正在关注https://www.sitepoint.com/using-socket-io-and-cordova-to-create-a-real-time-chat-app/聊天应用程序。还有应用程序聊天,这对我来说很好。
这是我的服务器端代码
socket.on('send:message', function (msg) {
console.log("send:message", msg);
if (msg.orderChat) var q = models.orderChat.create(msg)
else var q = models.chat.create(msg)
q.then(function (ret) {
console.log(ret.get({ plain: true }));
socket.in(msg.chat_room).emit('send:message', msg);
//socket.emit('send:message', msg);
}).catch(function (err) {
console.log(err);
socket.in(msg.chat_room).emit('send:message', 'Server Error');
});
});
这是我的客户端代码
socket.on('send:message', function (msg) {
username = $('.pchat').attr('data-username');
console.log(msg);
$("#chat_div_" + username).chatbox("option", "boxManager").addMsg(username, msg.message);
})
更新 当用户在同一浏览中聊天而不是在交叉浏览中进行聊天时,代码也能正常工作
答案 0 :(得分:1)
我相信这里提供的代码之外可能存在错误,
在您的服务器端,我已更改:
socket.in(msg.chat_room).emit('send:message', msg);
到
socket.in(msg.chat_room).emit('send:message', {
message: msg
});
虽然如果您能提供完整的代码,我将能够帮助您进一步排查问题。