会议室消息未与Socket.io&Express一起显示

时间:2018-10-07 22:15:50

标签: javascript jquery html express socket.io

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);
});
index.html

$(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警报。

1 个答案:

答案 0 :(得分:0)

我发现io.in(data.room)不被接受,因为data.room是一个JavaScript对象,而不是字符串。所以我刚添加,它就起作用了:

var room = data.room;
room = room.toString();