How to use RxJs with Socket.IO on event

后端 未结 6 909
温柔的废话
温柔的废话 2021-02-02 12:56

I want to use RxJS inside of my socket.on(\'sense\',function(data){});. I am stuck and confused with very few documentation available and my lack of understanding R

6条回答
  •  暖寄归人
    2021-02-02 13:23

    I have experienced some strange issues using the fromEvent method, so I prefer just to create my own Observable:

    function RxfromIO (io, eventName) {
      return Rx.Observable.create(observer => {
        io.on(eventName, (data) => {
            observer.onNext(data)
        });
        return {
            dispose : io.close
        }
    });
    

    I can then use like this:

    let $connection = RxfromIO(io, 'connection');
    

提交回复
热议问题