将更新通知发送给使用socket.io的特定用户

以下是前端的代码,其中storeSelUserId包含用于发送消息的user_id –

FYI – 节点版本1.1.0

 // Socket Notification var socket = io('http://localhost:6868'); socket.on('connection', function (data) { socket.emit('send notification', { sent_to: storeSelUserId }); }); 

以下是路由文件中的服务器代码 –

 var clients = {}; io.on('connection', function (socket) { socket.emit('connection', "Connection Created."); socket.on('send notification', function (sent_to) { console.log(sent_to); }); }); 

在控制台sent_to显示user_id的数组。

现在成为socket.io的入门者,我坚持解决scheme,我如何发送消息到这些特定的用户ID。

我search,发现我需要推动每个用户的sockets,所以我改革它 –

 var users = []; io.on('connection', function (socket) { users.push({socket_id: socket.id}); socket.emit('connection', "Connection Created."); socket.on('send notification', function (sent_to) { console.log(sent_to); }); }); 

但是,我处于两难的境地,我还需要做什么来存储哪个user_id引用哪个socket_id,然后用这个特定的ID更新用户的div?

编辑

添加控制器 – (前端)

前端创build备忘录的界面,并发送给特定用户

 var socket = io('http://localhost:6868'); socket.on('connection', function (data) { socket.emit('send memo notification', {creator_id: creator_id, sent_to: [Array of user_ids to whom memo to send]}); }); 

仪表板控制器 – (前端)

前端接口通知数显示“notificationCount”

 if (SessionService.currentUser._id) { var socket = io('http://localhost:6868'); socket.on('connection', function (data) { socket.emit('get notifications', {user_id: SessionService.currentUser._id}); }); socket.on('notification data', function(data){ console.log("-- Not Data Test -"); $scope.notificationCount = data.length; }); } 

代码在服务器端 –

 io.on('connection', function (socket) { socket.emit('connection', "Connection Created."); socket.on('send memo notification', function(data) { notifications.createNotification(data); }); socket.on('get notifications', function(data){ notifications.getNotifications(data, function(response){ socket.emit('notification data', response.data); }); }); }); 

后端控制器代码

 exports.getNotifications = function(data, callback) { var userId = data.user_id; Notification.find({receiver_id: userId}, function(err, response){ if (err) callback({"message": "error", "data": err, "status_code": "500"}); else callback({"message": "success", "data": response, "status_code": "200"}); }); }; exports.createNotification = function(data) { var notificationData = data; var x = 0; for(var i=0; i< notificationData.length; i++) { // Code Notification(notificationData[i]).save(function(err,response){ if (err) return false; }); if (x === notificationData.length - 1) { return true; } x++; } }; 

如果你想使用自己的用户ID,那么就没有办法将套接字ID映射到用户ID。 我假设一个客户端从某个地方知道它的用户ID,所以它可以在连接之后将其用户ID发送到服务器。

客户

 socket.on('connection', function (data) { socket.emit('setUserId', myUserId); }); 

服务器保存每个用户ID的套接字。

 socket.on('setUserId', function (userId) { users[userId]=socket; }); 

如果在服务器中有这样的映射,则可以使用用户标识仅向该客户端发送消息。

 socket.on('send notification', function (userId) { users[userId].emit('notification', "important notification message"); }); 

编辑:直接保存对应的套接字就更好了。

据我所知,你需要私人通知发送给只有一些用户。 为此,请将用户名称保存到不同的散列中,并将其发送给谁。

 username [socket.name] = username to be added; usersocket [ socket.name ] =socket; 

然后只发送消息给该用户,使用

 usersocket[ socket.name ].emit('event for send message', ' what you want to send ');