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

Flutter SignalR method not being invoked correctly #36

Open
ameeee opened this issue Apr 17, 2020 · 4 comments
Open

Flutter SignalR method not being invoked correctly #36

ameeee opened this issue Apr 17, 2020 · 4 comments

Comments

@ameeee
Copy link

ameeee commented Apr 17, 2020

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
Copy link

tvanhuu commented Sep 21, 2020

I have the same problem.

@ghulamostafa
Copy link

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
Copy link

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

@dvshin
Copy link

dvshin commented Feb 23, 2021

https://github.com/dvshin/signalr_client

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

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

5 participants