connect method
Future<Either<BaseException, void>>
connect(
{ - required String path,
- required String chatId,
- required void parser(
- Map<String, dynamic> json
),
- required void onStateUpdated(
- HubConnectionState state
),
})
Implementation
Future<Either<BaseException, void>> connect({
required String path,
required String chatId,
required void Function(Map<String, dynamic> json) parser,
required void Function(HubConnectionState state) onStateUpdated,
}) async {
final chatUrl = '${EnviromentUtils.to.hubUrl}$path?chatId=$chatId&access_token=$getAccessToken';
SeedKitLoggerService.i('Hub Connect: $chatUrl');
_controller = HubConnectionBuilder()
.withUrl(
chatUrl,
options: HttpConnectionOptions(
transport: HttpTransportType.WebSockets,
skipNegotiation: true,
),
)
.withAutomaticReconnect(retryDelays: [0, 2000, 5000, 10000, 30000])
.build();
try {
_stateSubscription = _controller?.stateStream.listen((state) => onStateUpdated(state));
_controller?.on(messageReceivedKey, (arguments) {
if (arguments != null && arguments.isNotEmpty) {
parser(arguments.firstOrNull as Map<String, dynamic>);
SeedKitLoggerService.i('Hub Message Received: ${arguments.firstOrNull}');
}
});
_controller?.onclose(({error}) {
SeedKitLoggerService.i('Hub Connection Closed: error:$error');
});
_controller?.onreconnecting(({error}) {
SeedKitLoggerService.w('Hub Reconnecting...: $error');
});
_controller?.onreconnected(({connectionId}) async {
SeedKitLoggerService.i('Hub Reconnected: $connectionId');
});
await _controller?.start();
SeedKitLoggerService.i('Hub Connected');
return const Right(null);
} catch (e, s) {
return ErrorHandler.fromError(e, stackTrace: s);
}
}