subscribe<T> method
Adds a subscriber to the Stream associated with the subscribe response.
Implementation
WebSocketSubscription<T> subscribe<T>(final JsonRpcSubscribeResponse response) {
// Debug the response.
assert(response.id != null, 'The response.id must not be null.');
assert(response.isSuccess, 'The response must contain a result.');
// The subscription id.
final int subscriptionId = response.result!;
// The request/response id.
final int exchangeId = response.id!;
// Get or create a stream controller for the subscription.
StreamController<T>? controller = _controller<T>(subscriptionId: subscriptionId);
if (controller == null || controller.isClosed) {
final MultiKey<int> key = MultiKey([subscriptionId, exchangeId]);
_subscriptionIdToController[key] = controller = StreamController.broadcast(sync: true);
}
// Create a stream listener.
return WebSocketSubscription(
subscriptionId,
exchangeId: exchangeId,
streamSubscription: controller.stream.listen(null),
);
}