2016-12-09 179 views
1

我使用socket.io從用戶(應用程序)聊天管理員(到瀏覽器)。這裏管理員信息已被正確接收給用戶,但管理員沒有收到用戶信息。我正在關注聊天應用程序的https://www.sitepoint.com/using-socket-io-and-cordova-to-create-a-real-time-chat-app/。還有應用程序應用程序聊天,這對我來說工作正常。socket.io應用到網絡聊天

這裏是我的服務器端代碼

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); 
}) 

更新 同樣的代碼工作正常時,在同一個瀏覽用戶聊天,但不在橫向瀏覽

回答

1

我相信有機會出現在這裏提供的代碼之外的錯誤,

在服務器端,我已經改變:

socket.in(msg.chat_room).emit('send:message', msg); 

socket.in(msg.chat_room).emit('send:message', { 
      message: msg 
     }); 

但如果你能提供完整代碼我將能夠幫助您進一步排除故障。