Hello, I wanted to replace the use of standard ws connection with web_socket_channel package, but ran into the problem of not being able to catch an exception on connection. Catch block does not want to trigger in any way. Can you explain how to achieve exception catching?
tryConnects(List<AccountServer> servers) async { for (var server in servers) { if (server.serialNumber == null) continue; var address = 'wss://test-home-${server.serialNumber!.substring(server.serialNumber!.length - 8)}.local'; try { var channel = IOWebSocketChannel.connect(Uri.parse(address), connectTimeout: Duration(seconds: 5)); await channel.sink.done.then((value) => null).catchError((e) { Log.error("Ошибка подключения к $address - ${e.message}"); }); Log.success("Успешно подключен к $address"); // return true; // Connected successfully } catch (error, stackTrace) { if (error is WebSocketChannelException) { Log.error("Ошибка подключения к $address - ${error.message}"); // return false; } else { Log.error("Ошибка подключения к $address - $error"); Log.error(stackTrace.toString()); } } } }
And that's what I get
Hello, I wanted to replace the use of standard ws connection with web_socket_channel package, but ran into the problem of not being able to catch an exception on connection. Catch block does not want to trigger in any way. Can you explain how to achieve exception catching?
tryConnects(List<AccountServer> servers) async { for (var server in servers) { if (server.serialNumber == null) continue; var address = 'wss://test-home-${server.serialNumber!.substring(server.serialNumber!.length - 8)}.local'; try { var channel = IOWebSocketChannel.connect(Uri.parse(address), connectTimeout: Duration(seconds: 5)); await channel.sink.done.then((value) => null).catchError((e) { Log.error("Ошибка подключения к $address - ${e.message}"); }); Log.success("Успешно подключен к $address"); // return true; // Connected successfully } catch (error, stackTrace) { if (error is WebSocketChannelException) { Log.error("Ошибка подключения к $address - ${error.message}"); // return false; } else { Log.error("Ошибка подключения к $address - $error"); Log.error(stackTrace.toString()); } } } }
And that's what I getE/flutter (12109): #0 _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:682:35) E/flutter (12109): #1 _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1827:26) E/flutter (12109): #2 RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23) E/flutter (12109): #3 RawSecureSocket.startConnect (dart:io/secure_socket.dart:299:22) E/flutter (12109): #4 SecureSocket.startConnect (dart:io/secure_socket.dart:77:28) E/flutter (12109): #5 _ConnectionTarget.connect (dart:_http/http_impl.dart:2449:26) E/flutter (12109): #6 _HttpClient._getConnection.connect (dart:_http/http_impl.dart:2867:12) E/flutter (12109): #7 _HttpClient._getConnection (dart:_http/http_impl.dart:2872:12) E/flutter (12109): #8 _HttpClient._openUrl (dart:_http/http_impl.dart:2727:12) E/flutter (12109): #9 _HttpClient.openUrl (dart:_http/http_impl.dart:2591:7) E/flutter (12109): #10 _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1021:42) E/flutter (12109): #11 WebSocket.connect (dart:_http/websocket.dart:320:22) E/flutter (12109): #12 new IOWebSocketChannel.connect io.dart:81 E/flutter (12109): #13 SocketController.tryConnects new_socket.dart:16 E/flutter (12109): #14 AuthController.attempt auth.dart:84 E/flutter (12109): <asynchronous suspension> E/flutter (12109):