我在Express
服务器Socket.io socket.broadcast.to不工作
userbase = {'Fitz': '84Iw3MNEIMaed0GoAAAD'}; //Exapmle user to receive message and associated socket id
//Sending message to Example user
socket.on('send_msg',function(data_from_client){
//Testing for receivers socketId
console.log(userbase[data_from_client.to_user]);//This returns the socket id for Fitz in userbase successfully i.e 84Iw3MNEIMaed0GoAAAD
socket.broadcast.to(userbase[data_from_client.to_user]).emit('get_msg',{msg:data_server.msg});
});
惊喜惊喜,当我安装一个处理程序上我cliens侧这一事件为'get_msg'
我什么也得不到。
.factory('SocketFctry',function(){
var socket = io('http://localhost:3002')
return socket;
})
.controller('ChatCtrl', function($scope,SocketFctry) {
SocketFctry.on('get_msg',function(received_message){
console.log(received_message);
$scope.$apply();
})
});
我的其他客户端处理程序工作正常。
SocketFctry.on('new_user', function(users,my_id){
console.log(users);
$scope.online_users = users;
$scope.$apply();
})
我的版本的socket.io是1.3.7。我在这里丢失了什么?
我unsderstand每个连接的客户端会自动添加到房间与他的插座#ID ...您的解决方案使用'socket.in(socket.id)对我来说'工作,但我米仍然难倒'socket.broadcast.to(socket.id)'不起作用。我真的很想理解为什么在接受答案之前。任何想法? – yaboiduke
看看这个http://stackoverflow.com/questions/6873607/socket-io-rooms-difference-between-broadcast-to-and-sockets-in#answer-6877212 – gyc