尝试在 main() 中使用 SignalR:
final connection = HubConnectionBuilder().withUrl('https://localhost:44320/chat-hub',
HttpConnectionOptions(
logging: (level, message) => print(message),
)).build();
await connection.start();
connection.on('ReceiveMessage', (message) {
print(message.toString());
});
这有效,握手成功,我从服务器收到一条消息,但随后它决定致命崩溃:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: The remote computer refused the network connection.
#0 IOClient.send (package:http/src/io_client.dart:94:7)
<asynchronous suspension>
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:32)
<asynchronous suspension>
#2 _withClient (package:http/http.dart:166:12)
<asynchronous suspension>
#3 _MainPageState.initState.<anonymous closure> (package:multipanel_chatwoot/presentation/pages/home_page.dart:22:24)
<asynchronous suspension>
我用谷歌搜索它,它说将 localhost 更改为你的 ip,所以我将其更改为:
...withUrl('https://192.168.1.39:44320/chat-hub'...
但是在发送协商请求时我收到以下信息:
flutter: Failed to complete negotiation with the server: HandshakeException: Handshake error in client (OS Error:
CERTIFICATE_VERIFY_FAILED: Hostname mismatch(../../third_party/boringssl/src/ssl/handshake.cc:393))
flutter: Failed to start the connection: HandshakeException: Handshake error in client (OS Error:
CERTIFICATE_VERIFY_FAILED: Hostname mismatch(../../third_party/boringssl/src/ssl/handshake.cc:393))
flutter: HubConnection failed to start successfully because of error '{HandshakeException: Handshake error in client (OS Error:
CERTIFICATE_VERIFY_FAILED: Hostname mismatch(../../third_party/boringssl/src/ssl/handshake.cc:393)).toString()}'.
所以我尝试将 https 更改为 http:
flutter: Failed to complete negotiation with the server: Connection closed before full header was received
谷歌搜索后,建议添加以下内容(当然仅适用于本地开发而不是生产使用):
// in main()
HttpOverrides.global = MyHttpOverrides();
class MyHttpOverrides extends HttpOverrides{
@override
HttpClient createHttpClient(SecurityContext? context){
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
}
}
但后来我得到:
flutter: Failed to start the connection: Exception: Unexpected status code returned from negotiate '400'
如果我尝试将
skipNegotiation: true
添加到 HttpConnectionOptions
那么我会得到 flutter: Failed to start the connection: Exception: Negotiation can only be skipped when using the WebSocket transport directly.
看起来你的代码已经完成了,没什么问题我可以看到你的代码。不要改变别人。现在只编辑以下部分。
Use http://10.0.2.2 instance of https://192.168.1.39.