【发布时间】: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