颤动websocket连接问题

时间:2018-05-08 07:13:35

标签: asp.net-core websocket dart flutter

我正在尝试开发一个连接到服务器并使用websocket交换数据的flutter应用程序。服务器位于.Net Core中,并使用Asp.Net Core Websockets实现此功能。

我面临的问题是,我的扑克应用程序无法连接到服务器并引发跟踪错误。

E/flutter (31498): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (31498): WebSocketChannelException: WebSocketChannelException: WebSocketException: Connection to 'http://127.0.0.1/client#' was not upgraded to websocket
E/flutter (31498): #0      new IOWebSocketChannel._withoutSocket.<anonymous closure> (package:web_socket_channel/io.dart:83:24)
E/flutter (31498): #1      _invokeErrorHandler (dart:async/async_error.dart:13:29)
E/flutter (31498): #2      _HandleErrorStream._handleError (dart:async/stream_pipe.dart:286:9)
E/flutter (31498): #3      _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:168:13)
E/flutter (31498): #4      _RootZone.runBinaryGuarded (dart:async/zone.dart:1328:10)
E/flutter (31498): #5      _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:350:15)
E/flutter (31498): #6      _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:367:16)
E/flutter (31498): #7      _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:266:7)
E/flutter (31498): #8      _SyncStreamController._sendError (dart:async/stream_controller.dart:767:19)
E/flutter (31498): #9      _StreamController._addError (dart:async/stream_controller.dart:647:7)
E/flutter (31498): #10     _RootZone.runBinaryGuarded (dart:async/zone.dart:1328:10)
E/flutter (31498): #11     _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:350:15)
E/flutter (31498): #12     _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:367:16)
E/flutter (31498): #13     _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:266:7)
E/flutter (31498): #14     _SyncStreamController._sendError (dart:async/stream_controller.dart:767:19)
E/flutter (31498): #15     _StreamController._addError (dart:async/stream_controller.dart:647:7)
E/flutter (31498): #16     new Stream.fromFuture.<anonymous closure> (dart:async/stream.dart:110:18)
E/flutter (31498): #17     _RootZone.runBinary (dart:async/zone.dart:1386:54)
E/flutter (31498): #18     _FutureListener.handleError (dart:async/future_impl.dart:141:20)
E/flutter (31498): #19     Future._propagateToListeners.handleError (dart:async/future_impl.dart:649:47)
E/flutter (31498): #20     Future._propagateToListeners (dart:async/future_impl.dart:670:24)
E/flutter (31498): #21     Future._completeError (dart:async/future_impl.dart:489:5)
E/flutter (31498): #22     Future._asyncCompleteError.<anonymous closure> (dart:async/future_impl.dart:537:7)
E/flutter (31498): #23     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (31498): #24     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

重要的是,该应用程序能够连接到其他websocket服务器。它还可以连接到我开发的用于在同一台机器上进行测试的其他原型服务器。

.Net核心服务器也非常实用,我可以轻松地将浏览器连接到服务器并与之通信。

只有当我尝试将flutter应用程序连接到.Net核心websocket服务器时才会出现问题。

我正在使用默认模板应用进行测试。这是我的MyHomePage类的代码

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final channel = new IOWebSocketChannel.connect("ws://192.168.1.139/client");

这是_MyHomePageState实现

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

initState() {
        super.initState();
        widget.channel.stream.listen(this.onData, onError: onError, onDone: onDone);

    (() async {
      setState(() {

      });
    });
  }

  onDone(){
    debugPrint("Socket is closed");
  }

  onError(err){
    debugPrint(err.runtimeType.toString());
    WebSocketChannelException ex = err;
    debugPrint(ex.message);
  }

  onData(event){
    debugPrint(event);
  }

总是导致onError并带有以下输出

I/flutter ( 4608): WebSocketChannelException
I/flutter ( 4608): WebSocketChannelException: WebSocketException: Connection to 'http://18.217.117.92/client#' was not upgraded to websocket
I/flutter ( 4608): Socket is closed

我知道它必须对dart websocket包如何执行与服务器的握手有关,但无法确定问题。

任何人都可以在这里弄清楚到底出了什么问题吗?

2 个答案:

答案 0 :(得分:0)

我知道这是一个古老的问题,但是如果有人遇到相同的问题,我可以通过在Windows中添加此system environment variable来解决它,并且为我解决了这个问题

enter image description here

注意:如果您使用的是Visual Studio代码,则需要重新启动它才能使此解决方案正常工作

答案 1 :(得分:0)

我希望这将对很多人有帮助,尽管该问题不再重要。问题在于.Net核心websocket的实现。

通过一些研究并分析了Wireshark日志后,我可以意识到.Net核心以某种格式期望websocket协议更新标头(即实现区分大小写),这对于.Net核心框架2.1而言是正确的。但是,该问题已在下一版本中立即得到解决。我改用Beta版,此问题已解决。我就此向Flutter开发人员和.Net核心团队进行了咨询,他们确认了同样的观点。

.Net核心的当前版本(在回答此问题时为3.0,没有此类问题。