[英]Flutter websockets working in local web but not in Firebase Hosting
I'm doing some tests with web_socket_channel Flutter plugin and I've noticed a very strange behavior.我正在用web_socket_channel Flutter 插件做一些测试,我注意到一个非常奇怪的行为。 I've implemented flutter-dev's example , just changing the socket kind to HtmlWebSocketChannel
in order to make it work in web builds.我已经实现了flutter-dev的示例,只是将套接字类型更改为HtmlWebSocketChannel
以使其在 web 构建中工作。 If I compile my app with flutter build web --release
and later I expose it with a local webserver, it works perfectly fine.如果我用flutter build web --release
编译我的应用程序,然后我用本地网络服务器公开它,它工作得很好。 Same happens if I execute it in debug mode.如果我在调试模式下执行它也会发生同样的情况。
However, if I deploy the release version to Firebase hosting ( firebase deploy
), the widgets where a HtmlWebSocketChannel
is present are rendered as a grey box.但是,如果我将发布版本部署到 Firebase 托管( firebase deploy
),则存在HtmlWebSocketChannel
的小部件将呈现为灰色框。 If I remove those instances, all widgets are rendered as usual.如果我删除这些实例,所有小部件都会照常呈现。 I thought Firebase hosting was nothing more that a very simple web server, I can't see how can it interfere with specific widgets in a Flutter app.我认为 Firebase 托管只不过是一个非常简单的 web 服务器,我看不出它如何干扰 Flutter 应用程序中的特定小部件。 Maybe the cause is related to the fact I'm accesing a remote URL?也许原因与我正在访问远程 URL 的事实有关?
Any help will be appreciated!任何帮助将不胜感激!
Here's the code of the app I'm deploying:这是我正在部署的应用程序的代码:
import 'package:flutter/foundation.dart';
import 'package:web_socket_channel/html.dart';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'WebSocket Demo';
return MaterialApp(
title: title,
home: MyHomePage(
title: title,
channel: HtmlWebSocketChannel.connect('ws://echo.websocket.org'),
),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
final WebSocketChannel channel;
MyHomePage({Key key, @required this.title, @required this.channel})
: super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Form(
child: TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: 'Send a message'),
),
),
StreamBuilder(
stream: widget.channel.stream,
builder: (context, snapshot) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 24.0),
child: Text(snapshot.hasData ? '${snapshot.data}' : ''),
);
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: Icon(Icons.send),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _sendMessage() {
if (_controller.text.isNotEmpty) {
widget.channel.sink.add(_controller.text);
}
}
@override
void dispose() {
widget.channel.sink.close();
super.dispose();
}
}
As mentioned above in my comments this seems to be an issue with trying to access insecure resource from a secure environment as https
.正如我在上面的评论中提到的,这似乎是尝试从安全环境中访问不安全资源的问题,例如https
。 Here is a working demo of the same code you used.这是您使用的相同代码的工作演示。
https://stackoverlfow-demos.web.app/#/ https://stackoverlfow-demos.web.app/#/
I just replaced it with wss
and deployed it to the firebase hosting.我只是将其替换为wss
并将其部署到 firebase 托管。
channel: HtmlWebSocketChannel.connect('wss://echo.websocket.org'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.