與Rx.Observable.webSocket
公開的Subject
有點麻煩。雖然WebSocket在complete
之後確實重新連接,但後續對Subject
的訂閱也會立即完成,而不會推送通過套接字發送的下一條消息。Rx.Observable.webSocket()在重新連接後立即完成?
我想我錯過了一些基本的關於這應該如何工作。
下面是一個requirebin /貼圖,希望能更好地說明我的意思,以及我期待的行爲。認爲這將是一個超級簡單的東西,我忽略了。
var Rx = require('rxjs')
var subject = Rx.Observable.webSocket('wss://echo.websocket.org')
subject.next(JSON.stringify('one'))
subject.subscribe(
function (msg) {
console.log('a', msg)
},
null,
function() {
console.log('a complete')
}
)
setTimeout(function() {
subject.complete()
}, 1000)
setTimeout(function() {
subject.next(JSON.stringify('two'))
}, 3000)
setTimeout(function() {
subject.next(JSON.stringify('three'))
subject.subscribe(
function (msg) {
// Was hoping to get 'two' and 'three'
console.log('b', msg)
},
null,
function() {
// Instead, we immediately get here.
console.log('b complete')
}
)
}, 5000)
聽起來像是你可能會涉及熱VS冷觀測。 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/creating.md#cold-vs-hot-observables – emc