An operator to cache a
WebSocketConnection until it closes, upon which the source that re-creates an HTTP
upgrade request to get a fresh
WebSocketConnection is subscribed, to refresh the stale connection in the
cache.
A typical usage example for this operator is:
HttpClient.newClient(socketAddress)
.createGet("/ws")
.requestWebSocketUpgrade()
.map(WebSocketResponse::getWebSocketConnection)
.nest()
.lift(new OperatorCacheSingleWebsocketConnection())
Since multiple subscriptions to
WebSocketResponse.getWebSocketConnection() do not re-run the original HTTP
upgrade request, this operator expects the source
Observable to be passed to it, so that on close of the
cached
WebSocketConnection, it can re-subscribe to the original HTTP request and create a fresh connection.
This is the reason the above code uses
Observable.nest() to get a reference to the source
Observable.
Cache liveness guarantees
Although, this operator will make sure that when the cached connection has terminated, the next refresh will
re-subscribe to the source, there is no guarantee that a dead connection is never emitted from this operator as it
completely depends on the timing of when the connection terminates and when a new subscription arrives. The two
events can be concurrent and hence unpredictable.