signalr_client icon indicating copy to clipboard operation
signalr_client copied to clipboard

Cannot connect to server

Open bwofsi opened this issue 3 years ago • 4 comments

Trying to connect to a dotnet6.0 SignalR server, I'm constantly getting this error in sensor.connect(): Cannot connect to sensor: Invocation canceled due to the underlying connection being closed. What is the issue? A C# client works like a charm.

main.dart:

import 'dart:io';
import 'sensor.dart';

void showText(String message) {
  print(message);
}

void finished(String message) {
  print(message);
}

Future<void> main() async {
  var sensor = Sensor(showText, finished, reconnect: false);
  try {
    var connected = await sensor.connect();
    var username = await sensor.identify();
    print("identified as ${username}");
    sleep(Duration(milliseconds: 500));
    var population = ["foo", "bar", "boo", "far"];
    var verified = await sensor.verify(username, population);
    print("verified: ${verified}");
  } on SensorException catch (e) {
    print('Cannot connect to sensor: ${e.inner}');
  }
}

sensor.dart:

import 'dart:io';
import 'dart:math';
import 'package:signalr_netcore/signalr_client.dart';

class Sensor {
  late HubConnection connection;
  bool reconnect;
  void Function(String) showText;
  void Function(String) finished;

  Sensor(this.showText, this.finished, {this.reconnect = true})
  {
    var builder = HubConnectionBuilder()
        .withUrl("https://localhost:53354/Sensor");
    if (reconnect)
      builder.withAutomaticReconnect();
    connection = builder.build();
    connection.onclose(({error}) {
      sleep(Duration(milliseconds: Random().nextInt(5) * 1000));
      connection.start();
    });
    if (reconnect) {
      connection.onreconnecting(({error}) {
        assert(connection.state == HubConnectionState.Reconnecting);
        showText("connection to sensor lost, reconnecting...");
      });
      connection.onreconnected(({connectionId}) {
        assert(connection.state == HubConnectionState.Connected);
        showText("reconnected to sensor");
      });
    }
  }

  Future<bool> connect() async {
    connection.on("ShowText", (text) {
      showText(text as String);
    });
    connection.on("Finished", (text) {
      finished(text as String);
    });
    while (true) {
      try {
        await connection.start();
        assert(connection.state == HubConnectionState.Connected);
        return true;
      } on Exception catch(e) {
        throw SensorException(e);
      }
    }
  }

  Future<String> identify({int sensorId = 0}) async {
    try {
      return await connection.invoke("Identify", args: [sensorId]) as String;
    } on Exception catch(e) {
      throw SensorException(e);
    }
  }

  Future<bool> verify(String user, List<String> population, {int sensorId = 0}) async {
    try {
      return await connection.invoke("Verify", args: [user, population, sensorId]) as bool;
    } on Exception catch(e) {
      throw SensorException(e);
    }
  }
}

class SensorException implements Exception {
  Exception inner;
  SensorException(this.inner);
}

bwofsi avatar Sep 07 '22 10:09 bwofsi

I've created simpler projects, including a C# client which works without any issues, but in the Dart client I now get [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed to invoke 'Identify' due to an error on the server. There's some traffic on the server but I can't get any valuable information from it. Maybe you're more skilled than me? tests_dartr.zip

bwofsi avatar Sep 08 '22 09:09 bwofsi

Same Here

ElKood-Sol avatar Sep 16 '22 21:09 ElKood-Sol

Fixed by going with docs of parameter on clients and server like KeepAliveInterval & you should increase the timeout of HttpConnectionOptions like this: final httpOptions = new HttpConnectionOptions(logger: transportProtLogger, requestTimeout: 15000, skipNegotiation: true, transport: HttpTransportType.WebSockets);

ElKood-Sol avatar Sep 16 '22 22:09 ElKood-Sol

Same problem (C# client works) and on flutter 'GeneralError (The underlying connection was closed before the hub handshake could complete.)' that happen when I add Authentication only

IdrisQashan avatar Sep 18 '22 14:09 IdrisQashan