Loopback + Socket.io自定义通知

时间:2016-07-06 09:44:41

标签: node.js express socket.io loopbackjs strongloop

我有模特:团队,项目,任务。项目,团队项目,团队中的任务由用户组成。

我在示例中创建了我的应用 - https://docs.strongloop.com/display/MSG/Building+a+real-time+app+using+socket.io+and+AngularJS

在我的例子中:

服务器/ server.js

...

app.use(loopback.token({ model: app.models.accessToken }));

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
  if (err) throw err;

  // start the server if `$ node server.js`
  if (require.main === module) {
    //Comment this app.start line and add following lines
    //app.start();
    app.io = require('socket.io')(app.start());
    require('socketio-auth')(app.io, {
      authenticate: function (socket, value, callback) {

        var AccessToken = app.models.AccessToken;
        //get credentials sent by the client
        var token = AccessToken.find({
          where:{
            and: [{ userId: value.userId }, { id: value.id }]
          }
        }, function(err, tokenDetail){
          if (err) throw err;
          if(tokenDetail.length){
            callback(null, true);
          } else {
            callback(null, false);
          }
        }); //find function..
      } //authenticate function..
    });

    app.io.on('connection', function(socket){
      console.log('a user connected');
      socket.on('disconnect', function(){
        console.log('user disconnected');
      });
    });
  }
});

服务器/ pubsub.js

'use strict';
var loopback = require('loopback');
//Writing pubsub module for socket.io
module.exports = {
  //Publishing a event..
  publish: function(socket, options ){

    var ctx = loopback.getCurrentContext();


    if(options){
      var collectionName = options.collectionName;
      var method         = options.method;
      var data           = options.data;
      var modelId        = options.modelId;
      if(method === 'POST'){
        //console.log('Posting new data');
        var name = '/' + collectionName + '/' + method;
        socket.emit(name, data);
      }
      else{
        var name = '/' + collectionName + '/' + modelId + '/' + method;
        socket.emit(name, data);
      }
    }else{
      throw 'Error: Option must be an object type';
    }
  }, //End Publish..

  isEmpty:function (obj) {
    var hasOwnProperty = Object.prototype.hasOwnProperty;
    // null and undefined are "empty"
    if (obj == null) return true;
    // Assume if it has a length property with a non-zero value
    // that that property is correct.
    if (obj.length > 0)    return false;
    if (obj.length === 0)  return true;
    // Otherwise, does it have any properties of its own?
    // Note that this doesn't handle
    // toString and valueOf enumeration bugs in IE < 9
    for (var key in obj) {
      if (this.hasOwnProperty.call(obj, key)) return false;
    }
    return true;
  } //isEmpty function..
}

公共/模型/ task.js

var pubsub = require('../../server/pubsub.js');
var loopback = require('loopback');
module.exports = function(Task) {
  //Task after save..
  Task.observe('after save', function (ctx, next) {
    console.log('Task after save');

    var socket = Task.app.io;
    if(ctx.isNewInstance){
      //Now publishing the data..
      pubsub.publish(socket, {
        collectionName : 'Task',
        data: ctx.instance,
        method: 'POST'
      });
    }else{
      //Now publishing the data..
      pubsub.publish(socket, {
        collectionName : 'Task',
        data: ctx.instance,
        modelId: ctx.instance.id,
        method: 'PUT'
      });
    }
    //Calling the next middleware..
    next();
  }); //after save..
  //TaskDetail before delete..
  Task.observe("before delete", function(ctx, next){
    var socket = Task.app.io;
    //Now publishing the data..
    pubsub.publish(socket, {
      collectionName : 'Task',
      data: ctx.instance.id,
      modelId: ctx.instance.id,
      method: 'DELETE'
    });
    //move to next middleware..
    next();
  }); //before delete..
}; //Module exports..

我想通过套接字交付任务,项目,团队变更。某些项目或任务可以是私有的。这意味着只有受邀的项目/任务成员才能看到它们。我在哪里可以让我的逻辑巫婆决定谁将收到通知?一般而言,所有团队成员都必须接收任务,项目和团队的变更,但在私人任务和项目中则是另一种逻辑。

最好的方法是什么?为常见案例创建类似team / team_id的名称空间或空间,并在私人案例中发送个人通知。或者更好的是为每个连接的用户创建名称空间或空间,以及在任务变更检查中谁必须接收更改并发送给他们?

在我的示例中,当我保存任务时,所有用户都通过套接字接收此任务...

感谢。

0 个答案:

没有答案