使用node.js、express、socket实现通知系统

使用node.js、express、socket实现通知系统,node.js,express,notifications,push-notification,socket.io,Node.js,Express,Notifications,Push Notification,Socket.io,我正在尝试添加一个通知系统。如果用户登录并执行某个操作,则应向另一个用户发送通知(如果他已登录)。我已经使用express在node.js中创建了我的应用程序。我知道我必须使用套接字,但是需要如何处理事件?如果有人能让我知道这个任务中我可以遵循的任何参考或样本?我只是做了一些类似的事情,并像这样处理它: /*connect to the server*/ var socket = io.connect(); /*do something wen the event 'user_did_acti

我正在尝试添加一个通知系统。如果用户登录并执行某个操作,则应向另一个用户发送通知(如果他已登录)。我已经使用express在node.js中创建了我的应用程序。我知道我必须使用套接字,但是需要如何处理事件?如果有人能让我知道这个任务中我可以遵循的任何参考或样本?

我只是做了一些类似的事情,并像这样处理它:

/*connect to the server*/
var socket = io.connect();

/*do something wen the event 'user_did_action'
 * is received, just invoke the callback
 *
 *in the function param data, you will have the same
 *data you emitted earlier in the server, 
 *in this example the user object!*/
socket.on('user_did_action', myFunctionToHandleTheEvent(data));
io.js-central node.js文件,我在其中处理新连接并存储io

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

io.on('connection', function(socket){
 console.log("Socket established with id: " + socket.id);

 socket.on('disconnect', function () {
  console.log("Socket disconnected: " + socket.id);
 });

});

module.exports = io;
someroute.js-我想在node.js服务器应用程序中的某个地方发出消息

var appRoot = require('app-root-path');
var io = require(appRoot + '/server/io');

/*your code, somwhere you will call the
 * function below whenever you want to emit
 * the 'user_did_action' event:
*/
if(user.didNewStuff()){
 emitUserAction(user);
}

var emitUserAction = function(user){
  io.sockets.emit('user_did_action', user);
};
client_javascript.js-一些客户端js(在我的例子中是角度的,但它并不重要),导入socket.io lib,然后访问客户端中的某个地方,如下所示:

/*connect to the server*/
var socket = io.connect();

/*do something wen the event 'user_did_action'
 * is received, just invoke the callback
 *
 *in the function param data, you will have the same
 *data you emitted earlier in the server, 
 *in this example the user object!*/
socket.on('user_did_action', myFunctionToHandleTheEvent(data));

不清楚缺少了什么部分。在我的聊天中,事件由socket.io客户端处理,我使用API将其显示给用户()@NKMY您尝试了什么吗?或者你想知道socket是如何工作的?你可以将此作为参考阅读。简而言之,你需要维护在线用户的全局列表及其各自的socket连接。因此,每当用户执行任何操作时,您都可以使用全局列表选择您的目标用户。感谢您的帮助。您是否知道提供了此套接字通知的实用程序类的任何引用或示例。您可以在node.js和socket.ioThanks@risuch链接中找到一个非常详细的通知应用程序教程/示例。