soernt / signalr_client

A Flutter SignalR Client for ASP.NET Core
MIT License
129 stars 202 forks source link

Flutter SignalR method not being invoked correctly #36

Open ameeee opened 4 years ago

ameeee commented 4 years ago

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

tvanhuu commented 4 years ago

I have the same problem.

ghulamostafa commented 3 years ago

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/

maxmitchenko commented 3 years ago

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

dvshin commented 3 years ago

https://github.com/dvshin/signalr_client

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