How to unsubscribe from a socket.io subscription?

前端 未结 11 1340
长发绾君心
长发绾君心 2020-11-30 02:00

Suppose there are objects making subscriptions to a socket server like so:

socket.on(\'news\', obj.socketEvent)

These objects have a short life

11条回答
  •  鱼传尺愫
    2020-11-30 02:22

    Socket.io version 0.9.16 implements removeListener but not off.

    You can use removeListener instead of off when unsubscribing, or simply implement off as follows:

      var socket = io.connect(url);
      socket.off = socket.removeListener;
    

    If you are using the Backbone listenTo event subscription approach, you'll need to implement the above as Backbone calls off when unsubscribing events.

提交回复
热议问题