AS-Devs / signalr_flutter

A flutter plugin for .net SignalR client.
https://pub.dev/packages/signalr_flutter
MIT License
18 stars 28 forks source link

There was an error invoking Hub method 'notificationHub.sendNotifications' #5

Closed AliEasy closed 3 years ago

AliEasy commented 4 years ago

I finally managed to get "Connected" status from signalr. Now when I try to invoke hub method I get below error:

I/flutter (15602): Platform Error: There was an error invoking Hub method 'notificationHub.notificationHub'.
E/flutter (15602): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: There was an error invoking Hub method 'notificationHub.notificationHub'.
E/flutter (15602): 

My code:(Error occurs when calling method _buttonTapped())

SignalR signalR;
 final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

 @override
 void initState() {
   super.initState();
   initPlatformState();
 }

 Future<void> initPlatformState() async {

    Map<String, String> header = {"username": "3"};

   signalR = SignalR('http://xxxxxxx/', "notificationHub",
       statusChangeCallback: _onStatusChange,
       hubCallback: _onNewMessage,
       headers: header );
 }

 _onStatusChange(String status) {
   if (mounted) {
     setState(() {
       _signalRStatus = status;
     });
   }
 }

 _onNewMessage(dynamic message) {
   print(message);
 }

 _buttonTapped() async {
   final res =
       await signalR.invokeMethod("sendNotifications");
   final snackBar =
       SnackBar(content: Text('SignalR Method Response: ${res.toString()}'));
   _scaffoldKey.currentState.showSnackBar(snackBar);
 }

My .Net Code:

namespace Mobile.Hubs
{
    [HubName("notificationHub")]
    public class NotificationHub : Hub
    {
         [HubMethodName("sendNotifications")]
         public void SendNotifications() 
        {
           ............
        }

        public override Task OnConnected()
        {
            connect();
            return base.OnConnected();
        }
        private void connect()
        {
            var userName = Context.QueryString["username"] ?? null;
            .........
        }
    }
}

OnConnected() method gets called when signalr status is "Connected".

AyonAB commented 4 years ago

I'll need more information on that. By the looks of it, that error was thrown by your signalr server. I can be wrong. Did you try to debug your server code?

AliEasy commented 4 years ago

Yes I did debug my code. At first when signalr gets connected, OnConnected() method gets called and after that connect().Every this goes ok and connect() call completes with no error. But after that nothing happens. SendNotifications() wont get called.

Another thing is that currently Im using this .Net code for my android application developed by Java and its working just fine. (When debugging .Net code on my android application, after connect() call gets complete and after signalr is connected, SendNotifications() gets called)

AliEasy commented 3 years ago

I solved my Problem Using newer version of plugin

Map<String, String> header = {"username": "3"};
    _signalR = SignalR(http://xxxxxxx/, "notificationHub",
        hubMethods: ['RecieveNotification'],
        statusChangeCallback: (status) => _onStatusChange(status),
        hubCallback: (methodName, message) {
          print(methodName);
          _connectionChange(message);
        },
        headers: header);
naeem-shah commented 3 years ago

I am facing same issue. how did you resolve your issue?