setupClient method
WebSocketChannel
setupClient()
Implementation
WebSocketChannel setupClient() {
if (_webSocketChannel != null) return _webSocketChannel!;
final channel = WebSocketChannel.connect(Uri.parse(endpoint));
channel.stream.listen((event) {
final data = jsonDecode(event);
if (data["error"] != null) {
streams[data["id"]]?.addError(data["error"]);
} else if (data["id"] != null && data["result"] != null) {
final reqId = data["result"];
final subId = data["id"];
subscriptionIdToRequestId[reqId] = subId;
requestIdToSubscriptionId[subId] = reqId;
} else {
final subId = data["params"]["subscription"];
final sm = streams[subscriptionIdToRequestId[subId]];
if (sm != null) {
sm.sink.add(event);
}
}
}, onError: (e) {
debugPrint(e);
}, onDone: () {
// for (var key in streams.keys) {
// streams[key]?.sink.close();
// }
});
_webSocketChannel = channel;
return _webSocketChannel!;
}