如何为本地 SignalR Web 服务器启用 SSL 以允许 flutter 连接到它?

How do I enable SSL for a local SignalR web server to allow flutter to connect to it?

提问人:Royal_Scribblz 提问时间:11/16/2023 更新时间:11/16/2023 访问量:23

问:

尝试在 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: trueHttpConnectionOptionsflutter: Failed to start the connection: Exception: Negotiation can only be skipped when using the WebSocket transport directly.

颤振 信号器

评论


答: 暂无答案