使用RxJS 6.x WebSocketSubject客户端

时间:2018-05-17 17:43:55

标签: websocket rxjs

我无法弄清楚如何在rxjs中使用WebSocketSubject v5.5.6

这是v6.x的工作HTML / JS。注释掉的代码是我试图让它在<html> <head> <!-- <script src="https://unpkg.com/@reactivex/rxjs@6.0.0/dist/global/rxjs.umd.js"></script> --> <script src="https://unpkg.com/@reactivex/rxjs@5.5.6/dist/global/Rx.js"></script> <script> // const { WebSocketSubject } = rxjs.webSocket; // const socket$ = WebSocketSubject.create('ws://localhost:8080'); const socket$ = Rx.Observable.webSocket('ws://localhost:8080'); socket$.subscribe( (data) => console.log(data), (err) => console.error(err), () => console.warn('Completed!') ); socket$.next(JSON.stringify({ event: 'events', data: 'test', })); console.log('here') </script> </head> <body></body> </html> 中运行:

@Transformer

1 个答案:

答案 0 :(得分:6)

我让它与rxjs@6.1.0合作。我怀疑,我只是使用版本6语法错误。见工作示例:

<html>

<head>
    <script src="https://unpkg.com/@reactivex/rxjs@6.1.0/dist/global/rxjs.umd.js"></script>
    <script>
        const { WebSocketSubject } = rxjs.webSocket;
        const socket$ = new WebSocketSubject('ws://localhost:8080');
        socket$.subscribe(
            (data) => console.log(data),
            (err) => console.error(err),
            () => console.warn('Completed!')
        );
        socket$.next({
            event: 'events',
            data: 'test',
        });
        console.log('here')
    </script>
</head>

<body></body>

</html>