dart-lang / dart-lang/http

Bad state: Stream has already been listened to.

Open
#1,526 2 comments 0 reactions 0 assignees View on GitHub
package:web_socket_channel
Dominant language
Dart
Stars
1.1k
Forks
419
Avg merge
4d 14h
Merged PRs (30d)
8

Description

I am getting an error: "Bad state: Stream has already been listened to." message every time I tried to reconnect the webstream when it gets a onDone event.
What is the correct way to reconnect the webstream if it gets disconnected?

// configure the websocket
Future configWebsocket(int _orgid, int userid, Map wsocket) async {
String host = wsocket['host'] + _orgid.toString() + wsocket['key'];
if (channel == null) {
channel = IOWebSocketChannel.connect(host);
_listenWebSocket( host);
print("SOCKET CHANNEL SET FOR " + _orgid.toString() + " Channel");
}
}

void _listenWebSocket( String host) {
if (channel != null) {
print("CHAT SOCKET LISTENING");
//_controller.stream = channel.stream;
// _controller.onListen((data) {
Stream _stream = channel.stream.asBroadcastStream();
_stream.listen((data) { // getting bad state error at this line
Map chatData = json.decode(data);
print("WEBSOCKET INCOMING DATA");
ChatCls chatdata = ChatCls.fromMap(chatData);
}, onError: (err) {
print(new DateTime.now().toString() + " Connection error: $err");
}, onDone: () async {
channel = IOWebSocketChannel.connect(host);
await Future.delayed(Duration(seconds: 4));
_listenWebSocket( host);
});
} else {
print("CHAT SOCKET NOT LISTENING");
}
}
}

Contributor guide

Open the contributing guide

Research direction

Start with configWebsocket and _listenWebSocket in the issue body, tracing the channel.stream subscription and the onDone reconnect path. Reproduce a disconnect and reconnect, then verify that the webstream reconnects without raising “Bad state: Stream has already been listened to.”

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.