How to unsubscribe from a socket.io subscription?

前端 未结 11 1341
长发绾君心
长发绾君心 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:25

    From looking at the source of socket.io.js (couldn't find it in documentation anywhere), I found these two functions:

    removeListener = function(name, fn)
    removeAllListeners = function(name)
    

    I used removeAllListeners successfully in my app; you should be able to choose from these:

    socket.removeListener("news", cbProxy);
    socket.removeAllListeners("news");
    

    Also, I don't think your solution of cbProxy = _blank would actually work; that would only affect the cbProxy variable, not any actual socket.io event.

提交回复
热议问题