What is the proper way to subscribe on the events in SignalR?
ingvilow opened this issue · 0 comments
I want to subscribe on events using signal r and using sound effect when these events happens. How can I create this in Dart? I am trying to create such logic: when a new id appears in websocket, a sound starts playing. In docs to signal r it only says that i should using hubconnection.on, but I don't understand how can I bond the new id and sound. And as sample they gave me this JS code above. Or may be I should use only argumet without object?
In Dart it looks like that. I have sample with data: data: {id: '1234', date: '2022-08-29T12:05:27.770Z', base: 'BaseNameExample', additionInfo: 'there is no example', dataId: '3fa85f64-5717-4562-b3fc-2c963f66afa6'},
and in Dart I am trying to do it like that using audioplayer:
class EventsSubsriber{
Alarmplayer alarmplayer = Alarmplayer();
Future<List> fetchNotifications() async {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
Logger? logger;
final httpConnectionOptions = HttpConnectionOptions(
accessTokenFactory: () => SharedPreferenceService().loginWithToken(),
skipNegotiation: true,
transport: HttpTransportType.WebSockets);
final hubConnection = HubConnectionBuilder()
.withUrl(
'http://securelink/home',
options: httpConnectionOptions,
)
.build();
await hubConnection.start();
List notifications= [];
if (hubConnection.state == HubConnectionState.Connected) {
await hubConnection
.invoke('GetNotifications')
.then((value) => notifications= value as List);
await alarmplayer.Alarm(url: 'assets/notifSound.mp3', volume: 1);
alarmplayer.IsPlaying();
}
hubConnection.on('GetNotifications', (arguments) {
if (arguments!.isNotEmpty) {
arguments == ['id'];
arguments == ['date'];
arguments == ['baseNameExample'];
arguments == ['additionInfo'];
alarmplayer.Alarm(url: 'assets/wanted.mp3', volume: 1);
alarmplayer.IsPlaying();
} else {
alarmplayer.StopAlarm();
}
});
// hubConnection.keepAliveIntervalInMilliseconds = 10 * 60 * 60 * 1000;
hubConnection.onclose(({error}) {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.error}: ${rec.message}');
});
print(error);
});
print(notifications);
return notifications;
}
}