简体   繁体   English

Flutter websockets 在本地 web 但不在 Firebase 中工作

[英]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.

相关问题 Flutter web 在 firebase 托管中不工作 - Flutter web not working in firebase hosting 我的 Flutter web 在本地构建得很好,但在 firebase 托管中不起作用 - My Flutter web build pretty well in local but doesn't work in firebase hosting Flutter Web Firebase Hosting XMLHttpRequest 错误(使用本地 chrome 浏览器(调试)或使用 Firefox 没有错误) - Flutter Web Firebase Hosting XMLHttpRequest error (no error using local chrome browser (debug), or using Firefox) Firebase 托管的 Flutter Web/Dart CORS 错误 - Flutter Web/Dart CORS Error with Firebase Hosting 带有 Firebase Auth 的 Flutter Web 无法在本地运行,但在部署在云中时可以运行 - Flutter Web with Firebase Auth not working in local but works when deployed in cloud Firebase 托管 - Flutter Web extra.ZFC35FDC70D5FC69D2539883A82EZC页面的应用程序托管 - Firebase Hosting - Flutter Web App hosting of extra .html page 为什么 firebase 不能与 flutter web 一起使用? - Why firebase not working with flutter web? 使用 Firebase 托管部署我的 Flutter Web 应用程序后出现错误 - Errors after deploying my flutter web app using Firebase Hosting 停止在缓存中保存 memory Flutter web Z035489FF8D092741943E4A83241F5AF9 - Stop saving in cache memory Flutter web Firebase hosting 发布 Flutter Web 应用程序到 Firebase 托管:最近的更改未发布 - Publishing Flutter Web App to Firebase Hosting: recent changes not published
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM