📅  最后修改于: 2022-03-11 14:44:45.151000             🧑  作者: Mango
wserror(err) async {
print(new DateTime.now().toString() + " Connection error: $err");
await reconnect();
}
reconnect() async {
if (_channel != null) {
// add in a reconnect delay
await Future.delayed(Duration(seconds: 4));
}
setState(() {
print(new DateTime.now().toString() + " Starting connection attempt...");
_channel = IOWebSocketChannel.connect(wsUrl);
print(new DateTime.now().toString() + " Connection attempt completed.");
});
_channel.stream.listen((data) => processMessage(data), onDone: reconnect, onError: wserror, cancelOnError: true);
}