index.js
socket.on('join room', function(data) {
var msg = "<span style='color:darkgreen;'>" + data.user + " has joined chat.</span>";
socket.to(data.room).emit('system message', msg);
});
$(function() {
socket.on('system message', function(msg) {
$('#messages').append($('<div class="systemMessage">').html(msg));
var height = $("#messages")[0].scrollHeight;
$.mobile.silentScroll(height);
});
});
$( document ).on( "pageshow", "#channel", function ( e ) {
var socket = io();
if (getCookie("username") == "null") {
$( ":mobile-pagecontainer" ).pagecontainer( "change", "#login-page", {
transition: "pop"
} );
}
else {
socket.emit('join room', { room: getCookie("currentRoom"), user: getCookie("username") });
$("#enter-sound").get(0).play();
}
});
因此,无论出于何种原因,在更改频道时,都不会显示用户通过“加入会议室”和“系统消息”加入新频道时发送的“系统消息”。为何会这样?我在其中放置了一个警报样式调试器,当系统消息socket.on被触发时,它不显示javascript警报。
答案 0 :(得分:0)
我发现io.in(data.room)
不被接受,因为data.room
是一个JavaScript对象,而不是字符串。所以我刚添加,它就起作用了:
var room = data.room;
room = room.toString();