2017-09-08 45 views
0

免责声明:我是新来的网络发展如此忍受我...WebSocketSubject - 优势?

堆栈:角前端,龙卷风(基于Python-)的后端

Web服务器使用我RxJs一直和WebSocket成功地与后端进行通信,基于这个非常流行的教程:https://medium.com/@lwojciechowski/websockets-with-angular2-and-rxjs-8b6c5be02fac

我刚刚在RxJs 5上偶然发现了WebSocketSubject,我想知道这些优点是什么?

到目前为止,我已经能够用它来连接,发送和接收,但我无法弄清楚如何使用典型RxJs运营商,我用一个主题得到...所以在某种程度上,它只是似乎很难使用。

我错过了什么?

这里是我的工作代码:我觉得我的困惑来自于缺乏RxJs基本面的理解

//create the socket 
    this.pubsubSubject = WebSocketSubject.create("ws://" + this.hostName + ":" + connection_info.port + "/" + connection_info.ps); 

    //output a message when it's open 
    this.pubsubSubject.openObserver = { 
    next: value => { 
     console.log("ps socket is " + (this.pubsubSubject.socket.readyState == WebSocket.OPEN ? "OPEN" : "NOT OPEN")); 
    } 
    } 

    //send the authentication token through the socket 
    this.pubsubSubject.next(JSON.stringify(authenticate_request)); 

//subscribe to specific events from server 
var subscription = { 
    "subscribe": events 
} 
this.pubsubSubject.next(JSON.stringify(subscription)); 

//start getting messages 
this.pubsubSubject.subscribe(
    (msg: any) => { 
    console.log("msg: " + msg); 
    } 
) 

回答

0

。但是,我没弄清楚如何在这种情况下映射,如下:

this.pubsubSubject 
    .map((resp: IPubSubMessage): any => { 
     console.log(resp.payload); 

    }).subscribe(); 

对于那些需要在WebSocketSubject特别的更多信息,你可以看看在代码中的注释,在这里:

https://github.com/mpodlasin/rxjs/blob/47ae8573256609492e16a957348883f0c8039c2e/src/observable/dom/WebSocketSubject.ts

我找不到这个细节在其他地方,所以这可能是找现在的最佳场所。

高兴有其他更多的信息,虽然插入内容。