delete image working

This commit is contained in:
gitea
2025-11-06 23:52:41 +01:00
parent 2c5f0eaefc
commit 84f1712916
4 changed files with 315 additions and 54 deletions
+50 -6
View File
@@ -127,7 +127,8 @@ class NostrService {
}
// WebSocketChannel.connect can throw synchronously (e.g., host lookup failure)
// Wrap in try-catch to ensure it's caught
// But errors can also occur asynchronously in the stream
// Wrap in try-catch to ensure synchronous errors are caught
WebSocketChannel channel;
try {
channel = WebSocketChannel.connect(Uri.parse(relayUrl));
@@ -139,8 +140,8 @@ class NostrService {
final controller = StreamController<Map<String, dynamic>>.broadcast();
_messageControllers[relayUrl] = controller;
// Update relay status (relay already found above)
relay.isConnected = true;
// Don't set isConnected = true immediately - wait for actual connection
// The connection might fail asynchronously
// Listen for messages
channel.stream.listen(
@@ -265,10 +266,53 @@ class NostrService {
throw Exception('Connection timeout');
},
);
// Start listening to establish connection, then cancel immediately
final subscription = stream.listen(null);
await Future.delayed(const Duration(milliseconds: 100));
// Wait for connection to be established or fail
// Listen to the stream to catch connection errors
final completer = Completer<bool>();
late StreamSubscription subscription;
bool gotFirstMessage = false;
subscription = stream.listen(
(data) {
// Connection successful - we received data
gotFirstMessage = true;
if (!completer.isCompleted) {
completer.complete(true);
}
},
onError: (error) {
// Connection failed
if (!completer.isCompleted) {
completer.complete(false);
}
},
onDone: () {
// Stream closed before connection established
if (!completer.isCompleted) {
completer.complete(false);
}
},
);
// Wait for connection to establish (first message) or fail
// Give it a short timeout to see if connection succeeds
final connected = await completer.future.timeout(
const Duration(seconds: 2),
onTimeout: () {
subscription.cancel();
// If we got a first message, connection was established
return gotFirstMessage;
},
);
subscription.cancel();
// Check if relay is actually connected
if (!connected || !relay.isConnected) {
results[relay.url] = false;
continue;
}
} catch (e) {
results[relay.url] = false;
continue;