【问题标题】:AngularJS socket.io event forwarding with deferred promiseAngularJS socket.io 事件转发与延迟承诺
【发布时间】:2016-01-26 03:53:04
【问题描述】:

我正在使用angular-socket-io,但需要一种方法在启动 socket.io 接口之前先在应用程序中进行身份验证。 I followed this method.

但是我需要在工厂as shown here这样使用事件转发:

socket.forward("event");

如何在我的工厂中添加转发?

.factory('socket', function($rootScope, $timeout, socketFactory, $q, $log) {
  // create a promise instance
  var socket = $q.defer();
  // listen for the authenticated event emitted on the rootScope of
  // the Angular app. Once the event is fired, create the socket and resolve
  // the promise.
  $rootScope.$on('authenticated', function() {

    // resolve in another digest cycle
    $timeout(function() {
      var token = window.localStorage["socket_token"];
      var token_query = 'token=' + token;
      var newSocket = (function() {
        return socketFactory({
          ioSocket: io.connect('http://example.com:3000', {
            query: token_query
          })
        });
      })();
      socket.resolve(newSocket);
    });
  });
  return socket.promise
});

【问题讨论】:

    标签: angularjs socket.io angular-promise


    【解决方案1】:

    您可以在使用创建的套接字解​​决承诺之前简单地将调用添加到forward()

    newSocket = (function() { /* ... */ })()
    newSocket.forward("event");
    socket.resolve(newSocket);
    

    【讨论】:

    猜你喜欢
    • 2015-03-08
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2015-03-27
    相关资源
    最近更新 更多