Using RxJS 6.x WebSocketSubject client

北战南征 提交于 2019-12-10 09:47:37

问题


I cannot figure out how to use WebSocketSubjects in rxjs v6.x

Here's the working HTML/JS for v5.5.6. The commented out code is my attempt at getting it working in v6.x:

<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>

回答1:


I got it working with rxjs@6.1.0. As I suspected, I was just using the version 6 syntax wrong. See working example:

<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>


来源:https://stackoverflow.com/questions/50397548/using-rxjs-6-x-websocketsubject-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!