Angular-socket-io:如何断开客户端连接

时间:2014-03-07 16:40:23

标签: javascript angularjs socket.io

更新

现在方法socket.disconnect(close) has been included in angular-socket-io。它有一个布尔参数'close',如果为true,也关闭底层连接。


我正在使用btford / angular-socket-io

断开客户端的正确方法是什么?

我正在尝试实施以下方案

  1. 用户登录 - >连接到套接字
  2. 用户注销 - >从插座断开连接
  3. 重复(1)(2)
  4. 我成功实现了connect(1),但我遇到了disconnect(2)

    的问题

    这就是我的尝试:在我的身份验证服务中,我有以下

    factory('AuthenticationService', function(socketFactory) {
      var mySocket;
      var service = {
          //...
          login: function(credentials) {
              var login = $http.post('/login', credentials);
              return login.then(function(response) {
                service.currentUser = response.data.user;
                if ( service.isAuthenticated() ) {
                    // **connect to socket on login** 
                    mySocket = socketFactory({ioSocket: io.connect('http://localhost')}); 
                }
                return service.isAuthenticated();
              });       
          },
    
          logout: function(redirectTo) {
              var logout = $http.get('/logout');
              logout.success(function() {
                service.currentUser = null;
                mySocket.disconnect();       // **disconnect on logout (not working)**
                redirect(redirectTo);
              });
              return logout;
          },
          //...
      };
      return service;
    })
    

    mySocket.disconnect();
    出现以下错误 TypeError:Object#没有方法'disconnect'

    如果不是

    mySocket.disconnect()有效

    mySocket = socketFactory({ioSocket: io.connect('http://localhost')});
    

    我用

    mySocket = io.connect('http://localhost'); 
    

3 个答案:

答案 0 :(得分:1)

您的代码看起来是正确的。我认为问题是变量提升,mySocket正在提升。 阅读here

答案 1 :(得分:1)

我的理解是btford/angular-socket-io将套接字实例包装在socketFactory中 但不公开disconnet()方法 因此它不能用于断开套接字与客户端的连接。

答案 2 :(得分:0)

事实上解决方案非常简单:

从Btford角度插座模块编辑“socket.js”文件,你会看到:

var wrappedSocket = {

          on: addListener,

          addListener: addListener,



          emit: function (eventName, data, callback) {

            return socket.emit(eventName, data, asyncAngularify(socket, callback));

          },    


          removeListener: function () {

            return socket.removeListener.apply(socket, arguments);

          },



          // when socket.on('someEvent', fn (data) { ... }),

          // call scope.$broadcast('someEvent', data)

          forward: function (events, scope) {

            if (events instanceof Array === false) {

              events = [events];

            }

            if (!scope) {

              scope = defaultScope;

            }

            events.forEach(function (eventName) {

              var prefixedEvent = prefix + eventName;

              var forwardBroadcast = asyncAngularify(socket, function (data) {

                scope.$broadcast(prefixedEvent, data);

              });

              scope.$on('$destroy', function () {

                socket.removeListener(eventName, forwardBroadcast);

              });

              socket.on(eventName, forwardBroadcast);

            });

          }

        };

然后你只需将其添加到其他功能旁边:

disconnect: function(){

                return socket.disconnect();

              },

瞧,你去吧:)。

你应该有类似的东西:

var wrappedSocket = {

          on: addListener,

          addListener: addListener,



          emit: function (eventName, data, callback) {

            return socket.emit(eventName, data, asyncAngularify(socket, callback));

          },



          disconnect: function(){

            return socket.disconnect();

          },



          removeListener: function () {

            return socket.removeListener.apply(socket, arguments);

          },



          // when socket.on('someEvent', fn (data) { ... }),

          // call scope.$broadcast('someEvent', data)

          forward: function (events, scope) {

            if (events instanceof Array === false) {

              events = [events];

            }

            if (!scope) {

              scope = defaultScope;

            }

            events.forEach(function (eventName) {

              var prefixedEvent = prefix + eventName;

              var forwardBroadcast = asyncAngularify(socket, function (data) {

                scope.$broadcast(prefixedEvent, data);

              });

              scope.$on('$destroy', function () {

                socket.removeListener(eventName, forwardBroadcast);

              });

              socket.on(eventName, forwardBroadcast);

            });

          }

        };