node.js - 了解 yeoman 全栈中的套接字服务

标签 node.js angularjs socket.io

我在理解 yeoman 的 fullstack 提供的套接字服务时遇到了一些困难。我提供了一个示例,说明如何使用syncUpdate 函数,添加 modelPlace 变量以了解从何处调用该函数。

所以,这是一个伪工作空间模型

var working_spaceSchema = new Schema({
...
users: [],
todos: [{name:String,info:String,priority:Number,id:Number}],
chat: [{content:String,poster:String,creation:Date}],
...
});

以及我在 Angular Controller 中调用套接字服务的方式:

$http.get('/api/works/' + $location.$$path.substr(7))
         .success(function(data) {
            $http.post('/api/works/' + $location.$$path.substr(7) + '/connexion', {type:0});
            $scope.chats = data;
            socket.syncUpdates('work', 'chat', $scope.chats, function(event, chat, chats) {
                $scope.chats = chats;
            });
        });

提醒一下,这里是套接字服务文件。您可以看到 modelPlace 变量,它使我能够知道必须从 schema.post 同步哪些内容:

'use strict';

angular.module('yoyoApp')
   .factory('socket', function(socketFactory) {

// socket.io now auto-configures its connection when we ommit a connection url
var ioSocket = io(null, {
  // Send auth token on connection, you will need to DI the Auth service above
  // 'query': 'token=' + Auth.getToken()
});

var socket = socketFactory({
  ioSocket: ioSocket
});

return {
  socket: socket,

  /**
   * Register listeners to sync an array with updates on a model
   *
   * Takes the array we want to sync, the model name that socket updates are sent from,
   * and an optional callback function after new items are updated.
   *
   * @param {String} modelName
   * @param {Array} array
   * @param {Function} cb
   */


  // Should consider givin' $state.params.id to syncUpdates
  // check currentUser.state and trigger notification IF =/= from update current state
  syncUpdates: function (modelName, modelPlace, array, cb) {
    cb = cb || angular.noop;

    /**
     * Syncs item creation/updates on 'model:save'
     */

    socket.on(modelName + ':save', function (item) {
      var event = 'created';
      if (modelPlace == 'todo_list')
        array = item.todos;
      else if (modelPlace == 'chat')
        array = item.chat;        

      cb(event, item, array);

    });

    /**
     * Syncs removed items on 'model:remove'
     * JE CROIS QUE CE N'EST PAS NECESSAIRE
     */
    socket.on(modelName + ':remove', function (item) {
      var event = 'deleted';
      _.remove(array, {_id: item._id});
      cb(event, item, array);
    });
  },

  /**
   * Removes listeners for a models updates on the socket
   *
   * @param modelName
   */
  unsyncUpdates: function (modelName, modelPlace) {
    socket.removeAllListeners(modelName + ':save:' + modelPlace);
    socket.removeAllListeners(modelName + ':remove:' + modelPlace);
  }
};
});

添加到“ioSocket变量”问题中,我想知道我应该如何使用syncUpdate函数来考虑最佳实践(我的不是,显然......)。

谢谢大家!

最佳答案

“to DI”是指使用依赖注入(inject)来获取Auth服务。Auth是Angular-Fullstack提供的另一项服务。只需在 Controller 之一的函数定义中包含对 Auth 的引用即可。例如:

angular.module('testSuiteUi2App')
    .controller('JobCtrl', function ($scope, Auth, socket) {
    $scope.myVar = 'Hello!';
}

我在执行您的代码时遇到了一些问题,但这就是我认为您想要的 socketUpdates 调用。

socket.syncUpdates('chat', $scope.chats);

这告诉系统您想要同步名为 $scope.chats 的“聊天”模型类型的数组。您还可以通过回调来完成此操作:

socket.syncUpdates('chat', $scope.chats, function(event, item, object) {
    $scope.chats = item;  // item contains the updated array
});

关于node.js - 了解 yeoman 全栈中的套接字服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25929829/

相关文章:

angularjs - Angular ui 路由器 - 在状态之间共享 View

javascript - AngularJs 在 ng-repeat 中显示/隐藏子标题

vue.js - 在vue js的main.js中使用async api

javascript - 如果我在 require() 中传递了太多变量会发生什么?

node.js - 如何使我的请求在node.js中同步?

angularjs - 将 MeteorJS 添加到现有的 AngularJS/MEAN 堆栈应用程序

node.js - 通过 socketio 发送 webRTC getUserMedia 网络摄像头流

email - 在 Node.js 中接收电子邮件

javascript - Node.js 中的箭头函数上下文

firebase - 如何将 socket.io 附加到 google firebase 应用程序功能?