2017-09-08 46 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

我找不到這個細節在其他地方,所以這可能是找現在的最佳場所。

高興有其他更多的信息,雖然插入內容。