我正在尝试开发一个颤振应用程序,它连接到服务器并使用websocket交换数据。服务器在中。Net内核,并使用Asp。Net核心WebSocket来实现此功能。
我面临的问题是,我的颤振应用程序无法连接到服务器,并抛出以下错误。
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 core服务器也非常实用,我可以轻松地将浏览器连接到服务器并与其通信。
只有当我尝试将颤振应用程序连接到时,问题才会出现。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");
以下是\u 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包如何执行与服务器的握手有关,但无法确定问题所在。
谁能找出这里到底出了什么问题吗?