58 lines
1.5 KiB
Dart
58 lines
1.5 KiB
Dart
|
|
import 'dart:async';
|
||
|
|
import 'dart:convert';
|
||
|
|
import 'package:swarm/swarm.dart';
|
||
|
|
|
||
|
|
final class FakeRelayConnection implements RelayConnection {
|
||
|
|
final _controller = StreamController<String>.broadcast();
|
||
|
|
final sentMessages = <String>[];
|
||
|
|
final queuedResponses = <String>[];
|
||
|
|
bool isConnected = false;
|
||
|
|
bool autoRespond = true;
|
||
|
|
bool throwOnConnect = false;
|
||
|
|
void Function(String subId)? onReq;
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<void> connect() async {
|
||
|
|
if (throwOnConnect) throw Exception('connection failed');
|
||
|
|
isConnected = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<void> send(String message) async {
|
||
|
|
sentMessages.add(message);
|
||
|
|
if (queuedResponses.isNotEmpty) {
|
||
|
|
final response = queuedResponses.removeAt(0);
|
||
|
|
Future(() => _controller.add(response));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
_tryAutoRespond(message);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Stream<String> get messages => _controller.stream;
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<void> close() async => isConnected = false;
|
||
|
|
|
||
|
|
void push(String message) => _controller.add(message);
|
||
|
|
|
||
|
|
Future<void> dispose() => _controller.close();
|
||
|
|
|
||
|
|
void _tryAutoRespond(String message) {
|
||
|
|
if (!autoRespond) return;
|
||
|
|
try {
|
||
|
|
final list = jsonDecode(message) as List;
|
||
|
|
if (list[0] == 'EVENT') {
|
||
|
|
final event = list[1] as Map<String, dynamic>;
|
||
|
|
final id = event['id'] as String;
|
||
|
|
Future(() => _controller.add(jsonEncode(['OK', id, true, ''])));
|
||
|
|
} else if (list[0] == 'REQ') {
|
||
|
|
final subId = list[1] as String;
|
||
|
|
Future(() => onReq?.call(subId));
|
||
|
|
}
|
||
|
|
} catch (_) {
|
||
|
|
// malformed, ignore
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|