【问题标题】:Send update notification to particular users using socket.io使用 socket.io 向特定用户发送更新通知
【发布时间】:2014-12-05 12:03:30
【问题描述】:

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

仅供参考 - 节点版本 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 的初学者,我坚持使用如何将消息发送到这些特定用户 ID 的解决方案。

我搜索并发现我需要用它的套接字推送每个用户,所以我将它改成了 -

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?

编辑 -

添加控制器 -(前端)

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

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++;
    }
};

【问题讨论】:

    标签: node.js sockets express socket.io mean-stack


    【解决方案1】:

    如果您想使用自己的用户 ID,则无法将套接字 ID 映射到用户 ID。我假设客户端从某个地方知道它的用户 ID,因此它可以在连接后将其用户 ID 发送到服务器。

    客户

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

    服务器为每个用户 id 保存套接字。

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

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

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

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

    【讨论】:

    • 请找到我的编辑..我没有在客户端自动更新..我必须刷新浏览器然后才更新反映
    【解决方案2】:

    据我了解,您只需要向部分用户发送私人通知。为此,请将您要发送的用户名及其对应的套接字保存在不同的哈希值中。

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

    然后要仅向该用户发送消息,请使用

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

    【讨论】:

    • 是的,让我为你绑定文件
    • 我看到了您的编辑,但我没有看到任何错误。尝试使用连接而不是连接,因为连接是一个保留事件。并且不要忘记在服务器中更改事件名称。
    猜你喜欢
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    相关资源
    最近更新 更多