使用socket.io向特定用户发送更新通知 [英] Send update notification to particular users using socket.io

查看:322
本文介绍了使用socket.io向特定用户发送更新通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following is the code on front end, where storeSelUserId contains user_id to send the message-

仅供参考-节点版本1.1.0

FYI - Node Version 1.1.0

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

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

Following is the server code in routes file -

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的数组.

In console sent_to is showing the array of user_id.

现在是socket.io的入门者,我坚持如何将消息发送给这些特定用户ID的解决方案.

Now being a starter in socket.io I stuck with the solution that how do I send the message to these particular userids.

我进行搜索,发现我需要用套接字推每个用户,因此我将其重新设置为-

I search and found that I need to push each user with its sockets so I reformed it to -

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?

But I am in dilemma that else do I need to do to store which user_id refers to which socket_id and then update the div of users with that particular ids?

编辑-

添加控制器-(前端)

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

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发送到服务器.

If you want to use your own user ids then there is no way around mapping the socket id to the user id. I assume a client knows its user id from somewhere, so it could send its user id to the server after connection.

客户

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

服务器为每个用户ID保存套接字.

The server saves the socket for each user id.

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

如果服务器中有这样的映射,则可以使用用户ID仅向该客户端发送一条消息.

If you have such a mapping in the server you can send a message just to this client using the user id.

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

直接保存相应的套接字更好.

Saving the corresponding socket directly is even better.

这篇关于使用socket.io向特定用户发送更新通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆