gpt4 book ai didi

android - 无法使用 Flutter 连接到 wss

转载 作者:行者123 更新时间:2023-12-04 22:35:00 28 4
gpt4 key购买 nike

我在 Nodejs 中编写了一个 wss 服务器,现在我正在尝试使用 Flutter 连接到这样的服务器。

这是 NodeJS 中的代码:

//Dependencies
const WebSocket = require('ws');
const fs = require('fs');
const https = require('https');
//Dependencies

//Server declarations
const server = https.createServer({
key: fs.readFileSync('pathTo/key.pem'),
cert: fs.readFileSync('pathTo/cert.pem')
});
server.listen(xxxx);
const wss = new WebSocket.Server({ server });
//Server declarations

wss.on('connection', function connection(ws)
{
ws.on('message', function incoming(message)
{
console.log('Received: ' + message);
ws.send('echo: ' + message);
});
ws.send('Connected!');
});

这是 Flutter 中的代码:
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:web_socket_channel/io.dart';
import 'package:connectivity/connectivity.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class MyApp extends StatelessWidget
{
@override
Widget build(BuildContext context)
{
final title = 'LumenApp Prototype';
IOWebSocketChannel channel;
try
{
channel = new IOWebSocketChannel.connect('wss://xxxxxxxx.xxx.xxx:xxxx/');
MyHomePageState.noResponse = false;
}
catch(e)
{
MyHomePageState.noResponse = true;
}
return MaterialApp(
title: title,
theme: ThemeData(
primarySwatch: Colors.blue,
primaryTextTheme: TextTheme(
title: TextStyle(
color: Colors.yellow[600],
),
),
),
home: MyHomePage(
title: title,
channel: channel,
),
);
}
}

Flutter 上的错误是:WebSocketChannelException: WebSocketChannelException: HandshakeException: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: self signed certificate(handshake.cc:354))

这发生在这个函数内部:
  void initPlatformState()
{
widget.channel.stream.listen((message)
{
setState(() { noResponse = false; });
//Handle message...
},
onError: (error)
{
print(error);
if(mounted)
{
setState((){ noResponse = true;});
}
},
onDone: ()
{
if(mounted)
{
setState((){ noResponse = true; });
}
});
}

我使用了一个用 openssl 制作的自签名证书服务器端。
知道如何解决这个问题吗?

最佳答案

如果你碰巧碰到this GitHub post ,您可以按照 this comment 中的临时修复:

class MyHttpOverrides extends HttpOverrides{
@override
HttpClient createHttpClient(SecurityContext context){
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
}
}

void main(){
HttpOverrides.global = new MyHttpOverrides();
runApp(new MyApp());
}

它适用于具有自签名证书的本地 IP。
详细说明, here is the same solution :

Just for the sake of clarity specially for the newcomers toFlutter/Dart, here is what you need to do in order to enable thisoption globally in your project:

  1. In your main.dart file, add or import the following class:
HttpClient createHttpClient(SecurityContext? context){
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port)=> true; } } ```

  • 在您的主函数中,在函数定义之后添加以下行:

  • HttpOverrides.global = MyHttpOverrides();


    This
    评论对通过这件事很有帮助,
    请注意...

    This should be used while in development mode, do NOT do this whenyou want to release to production, the aim of this answer is to makethe development a bit easier for you, for production, you need to fixyour certificate issue and use it properly, look at the other answersfor this as it might be helpful for your case.



    值得一提的另一件事是,签名证书现在免费提供 ( https://letsencrypt.org/)。
    另外,我认为 Flutter 团队正在努力 enhance the documentation为了更好地引用这个问题。正在跟踪 here .

    关于android - 无法使用 Flutter 连接到 wss,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580971/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com