简体   繁体   English

Node.js应该通知哪个用户

[英]Node.js which user should be notified

I built a notification system on my website. 我在网站上建立了一个通知系统。 I've some kind of groups of members. 我有一些成员团体。 When a member of a group posts something, the other members of the same group should be notified. 当某个组的成员发布内容时,应通知同一组的其他成员。 Notifications appear when the user refreshes the page. 用户刷新页面时出现通知。 However, the world is going in real-time. 但是,世界正在实时发展。 Googleing the issue recommended me Node.js (Not sure is the best way). 谷歌搜索的问题向我推荐了Node.js(不确定这是最好的方法)。 I can now: create a server, connect to a database, querying the database, broad messages using socket.io to all connected clients... 我现在可以:创建服务器,连接到数据库,查询数据库,使用socket.io向所有连接的客户端发送广泛消息...
But what I can't do/understand so far is to specify to which user(s) the message(notification) should be delivered. 但是,到目前为止,我仍然无法理解/是要指定将消息(通知)发送给哪个用户。
Looking around SO comes with this way that looks what I want: 环顾四周以这种方式呈现我想要的东西:

var socket = io.listen(server);
socket.on('connection', function(client) {
  client.on('message', function(msg) {
    broadcast(msg); // broadcast message to all clients
    // OR
    socket.clients[session_id].send(msg); // send message to one client
  });

  client.on('disconnect', function( ) {
    console.log('Client Disconnected.');
  });
});

I don't know what session_id is, is it the same as the session_id of the user created when he logs-in? 我不知道什么是session_id,与用户登录时创建的session_id相同吗? Really got tired looking for some straight forward solutions, any help out will be strongly appreciated. 寻找一些直截了当的解决方案真的很累,任何帮助将不胜感激。

Here's an example from socket.io docs: 这是来自socket.io文档的示例:

Storing data associated to a client 存储与客户端关联的数据

Sometimes it's necessary to store data associated with a client that's necessary for the duration of the session. 有时有必要存储与客户端相关联的数据,这在会话期间是必需的。

Server side 服务器端

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.on('set nickname', function (name) {
    socket.set('nickname', name, function () { socket.emit('ready'); });
  });

  socket.on('msg', function () {
    socket.get('nickname', function (err, name) {
      console.log('Chat message by ', name);
    });
  });
});

Client side 客户端

<script>
  var socket = io.connect('http://localhost');

  socket.on('connect', function () {
    socket.emit('set nickname', prompt('What is your nickname?'));
    socket.on('ready', function () {
      console.log('Connected !');
      socket.emit('msg', prompt('What is your message?'));
    });
  });
</script>

Create rooms, multiple clients can be a part of multiple rooms, and clients in a room can broadcast to all other members of the room. 创建会议室,多个客户可以是多个会议室的一部分,一个会议室中的客户端可以广播给会议室的所有其他成员。

Assuming your message has a room name in it and a data body 假设您的消息中有一个房间名称和一个数据正文

socket.on('connection', function(client) {
    client.on('message', function(msg) {
        // broadcast message to all clients
        io.sockets.in(msg.room).emit(msg.data);
        // OR
        socket.clients[session_id].send(msg); // send message to one client
    });

    client.on('disconnect', function( ) {
        console.log('Client Disconnected.');
    });
});

https://github.com/LearnBoost/socket.io This should have some examples https://github.com/LearnBoost/socket.io这应该有一些例子

http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/ This is a good one http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/这是一个不错的选择

Socket.IO is super easy to use. Socket.IO非常易于使用。
Look at the doc here: https://github.com/learnboost/socket.io/ 在此处查看文档: https : //github.com/learnboost/socket.io/

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.join('justin bieber fans');
  socket.broadcast.to('justin bieber fans').emit('new fan');
  io.sockets.in('rammstein fans').emit('new non-fan');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM