Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the proper way to subscribe on the events in SignalR? #94

Closed
ingvilow opened this issue Aug 29, 2022 · 0 comments
Closed

What is the proper way to subscribe on the events in SignalR? #94

ingvilow opened this issue Aug 29, 2022 · 0 comments

Comments

@ingvilow
Copy link

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;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants