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

Cannot connect to server #37

Open
bwofsi opened this issue Sep 7, 2022 · 4 comments
Open

Cannot connect to server #37

bwofsi opened this issue Sep 7, 2022 · 4 comments

Comments

@bwofsi
Copy link

bwofsi commented Sep 7, 2022

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

bwofsi commented Sep 8, 2022

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

@ElKood-Sol
Copy link

Same Here

@ElKood-Sol
Copy link

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);

@IdrisQashan
Copy link

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

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

3 participants