- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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!');
});
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,
),
);
}
}
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; });
}
});
}
最佳答案
如果你碰巧碰到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());
}
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:
- 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/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!