signalr_client icon indicating copy to clipboard operation
signalr_client copied to clipboard

Flutter SignalR method not being invoked correctly

Open ameeee opened this issue 4 years ago • 4 comments

This is my CommentHub in flutter:

// Import the library.
import 'package:signalr_client/signalr_client.dart';
import 'package:logging/logging.dart';
import 'package:xperience/models/global.dart';
// The location of the SignalR Server.
final serverUrl = "http://" + base + ":8070/CommentHub";

final hubConnection = HubConnectionBuilder().withUrl(serverUrl).build();

class HubService {
  static final hubProtLogger = Logger("SignalR - hub");
  static final  transportProtLogger = Logger("SignalR - transport");
  static final connectionOptions = HttpConnectionOptions;

  static final httpOptions = new HttpConnectionOptions(logger: transportProtLogger);
  HubConnection hubConnection = HubConnectionBuilder().withUrl(serverUrl, options: httpOptions).configureLogging(hubProtLogger).build();
  bool connectionIsOpen;

  Future<void> openChatConnection() async {
    if (hubConnection == null) {
      hubConnection.onclose((error) => connectionIsOpen = false);
    }

      await hubConnection.start();
      connectionIsOpen = true;

  }
  @override
  Future<void> executeTest(String json) async {

    hubConnection.on("AddUser", _handleServerInvokeMethodSimpleParametersNoReturnValue);
    try {
      await hubConnection.invoke("AddUser",args:<Object>[json]);
    } finally {
      hubConnection.off("AddUser", method: _handleServerInvokeMethodSimpleParametersNoReturnValue);
    }
  }

  void _handleServerInvokeMethodSimpleParametersNoReturnValue(List<Object> parameters) {

    final paramValues = new StringBuffer("Parameters: ");
    for(int i = 0; i < parameters.length; i++){
      final value = parameters[i];
      paramValues.write( "$i => $value, ");
    }

    print("From Callback: Server invoked method 'ServerInvokeMethodSimpleParametersNoReturnValue': " + paramValues.toString());
  }

  Future removeUser() async {
//    final result = await hubConnection.invoke("RemoveUser",args: <Object>[]);
//    print(result);
    hubConnection.stop();
    //hubConnection.onclose( (error) => print("Connection Closed"));

  }
}

This is my CommentHub.cs in asp.net core:

using System;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Xperience.Data.Entities.Posts;

namespace Xperience.Hub
{
    public class CommentHub : Microsoft.AspNetCore.SignalR.Hub
    {
        public static List<Connection> Connections = new List<Connection>();

        public void AddUser(string json) {
      
string []arr=json.split(",");
            Connections.Add(new Connection
            {
                ConnectionId = Context.ConnectionId,
                UserId = json[0],
                PostId = json[1]
            });
        }
        public void RemoveUser() {
            var user = Connections.Find(x => x.ConnectionId == Context.ConnectionId);
            Connections.Remove(user);
        }

    }

    public class Connection { 
        public string ConnectionId { get; set; }
        public string UserId { get; set; }
        public int PostId { get; set; }
    }
}

Can someone please tell me what's the wrong thing im doing as im always getting this error when i invoke executeTest:

Unhandled Exception: type 'GeneralError' is not a subtype of type 'Error' in type cast

ameeee avatar Apr 17 '20 14:04 ameeee

I have the same problem.

tvanhuu avatar Sep 21 '20 10:09 tvanhuu

Maybe your client arguments are not matching or their types are not matching with your server method. If the argument type in Server method is integer and you are trying to pass string, it would cause such thing. https://ghulamustafa.com/2021/01/16/how-to-use-signalr-with-flutter-for-chat/

ghulamostafa avatar Jan 15 '21 20:01 ghulamostafa

Has somebody fixed it? I have the same problem...

maxmitchenko avatar Feb 22 '21 15:02 maxmitchenko

https://github.com/dvshin/signalr_client

I have forked this project and modified myself. This works fine for my project.

dvshin avatar Feb 23 '21 23:02 dvshin