Skip to content

Commit 87b018f

Browse files
committed
await ready state of socket to fix exception issue #77
1 parent 5297450 commit 87b018f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

lib/relays.dart

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'dart:io';
2+
23
import 'dart:convert';
34
import 'package:nostr_console/event_ds.dart';
45
import 'package:nostr_console/settings.dart';
56
import 'package:nostr_console/utils.dart';
67
import 'package:web_socket_channel/io.dart';
8+
import 'package:web_socket_channel/src/exception.dart';
79

810
class Relay {
911
String url;
@@ -145,7 +147,7 @@ class Relays {
145147
/*
146148
* Send the given string to the given relay. Is used to send both requests, and to send evnets.
147149
*/
148-
void sendRequest(String relayUrl, String request) {
150+
void sendRequest(String relayUrl, String request) async {
149151
if(relayUrl == "" ) {
150152
if( gDebug != 0) print ("Invalid or empty relay given");
151153
return;
@@ -164,6 +166,15 @@ class Relays {
164166

165167
try {
166168
IOWebSocketChannel fws2 = IOWebSocketChannel.connect(relayUrl);
169+
170+
try {
171+
await fws2.ready;
172+
} catch (e) {
173+
// handle exception here
174+
//print("Error: Failed to connect to relay $relayUrl . Got exception = |${e.toString()}|");
175+
return;
176+
}
177+
167178
Relay newRelay = Relay(relayUrl, fws2, {}, 0, 1);
168179
relays[relayUrl] = newRelay;
169180
fws = fws2;
@@ -200,7 +211,11 @@ class Relays {
200211
} on WebSocketException {
201212
print('WebSocketException exception for relay $relayUrl');
202213
return;
203-
} on Exception catch(ex) {
214+
} on WebSocketChannelException {
215+
print('WebSocketChannelException exception for relay $relayUrl');
216+
return; // is presently not used/called
217+
}
218+
on Exception catch(ex) {
204219
printWarning("Invalid event\n");
205220
}
206221

0 commit comments

Comments
 (0)