【发布时间】:2016-08-12 13:14:30
【问题描述】:
我正在使用 nodejs 和 socket.io 开发多房间聊天系统。
这是我的服务器端脚本
socket.on('sendchat', function (data) {
socket.username = data.name;
// store the room name in the socket session for this client
socket.room = data.room;
// add the client's username to the global list
usernames[data.name] = data.name;
// send client to room
socket.join(data.room);
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.in(data.room).emit('updatechat',data.name, data);
});
还有客户端
socket.on('updatechat', function (username, data) {
var html = '<div class="media">'+
'<div class="media-left"> '+
'<a href="#"> '+
'<img class="media-object img-circle" data-src="holder.js/64x64" alt="64x64" '+
'src="assets/images/profile/'+data.img+'" data-holder-rendered="true" style="width: 64px; height: 64px;">'+
'</a>'+
'</div>'+
'<div class="media-body">'+
'<h4 class="media-heading">'+data.name+'</h4> <p>'+data.message+'</p><a class="chat-time">'+
'<i class="glyphicon glyphicon-time"></i>'+moment().format('MMMM Do YYYY, h:mm:ss a')+'</a></div>'+
'</div>';
$("#dash_threads").html(html); });
每当触发此事件时,它都会发生两次。我检查了所有可能的解决方案,但都没有奏效。谁能指导我哪里做错了。
【问题讨论】: