【问题标题】:Flutter Socket io Error on WebSocketException: Connection to was not upgraded to websocketWebSocketException上的Flutter Socket io错误:连接未升级到websocket
【发布时间】:2021-01-15 10:14:16
【问题描述】:

我使用this 包,它在测试网站上正常工作,但在应用程序中我得到了这个错误

WebSocketException: Connection to 'https://socket.excopro.com:0/socket.io/?EIO=3&transport=websocket#' was not upgraded to websocket

这是我的代码

SocketService() {
    var socket = io(
        'https://socket.excopro.com:443/', <String, dynamic>{
      'transports': ['websocket'],
      'autoConnect': true,
    });
    socket.on('connect', (_) {
      print('connect');
      socket.emit('msg', 'test');
    });
      socket.on("connecting", (data) => print('connecting'));
      socket.on('connect_error', (data) {
        print(data);
        socket.emit('msg', 'test');
      }); 
  }

【问题讨论】:

  • 我面临着类似的问题。有什么解决办法吗?
  • 是否可以以某种方式打印错误代码?

标签: flutter sockets dart socket.io


【解决方案1】:

我遇到了同样的问题。

就我而言,我使用 Nginx 作为代理。我通过在我的 Nginx 配置中添加一些代理标头来解决这个问题。

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

你可以参考这个link

【讨论】:

  • 嘿,我怎么能在共享 cpanel/hosting 中做到这一点
【解决方案2】:

我在由 NGinx + NChan 提供服务的 Flutter/Dart 应用程序中遇到了这个错误。事实证明,我在 Dart 中创建的 wss:... url 格式不正确 - 如果您在 Dart、PHP 和 JS 中进行字符串插值,则很容易在几分钟内完成,因为每种语言都有自己的解释方式大括号中的插值变量。结果是我的 Nginx/NChan conf 中的 location = ~ /path/(regex)$ 设置没有将 URL 识别为需要升级到 wss 的 URL。 Nginx 然后继续尝试在https://example.com/malformed/path 找到资源没有将其升级到wss,然后在 Dart 中抛出了这个错误。

课程 - 发生这种情况时,请检查您尝试访问的 URL 格式是否正确,以便最终将其识别为需要升级到 wssserver 端的 URL。

【讨论】:

    【解决方案3】:

    您已确保在虚拟主机中启用 websocket。

    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteRule /(.*)           ws://127.0.0.1:3055/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*)           http://127.0.0.1:3055/$1 [P,L]
    

    如果有人需要,这里是完整的虚拟主机,

    <VirtualHost *:80>
        ServerName domain.com
        ServerAlias www.domain.com
    
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
    
        <Proxy *>
            Require all granted
        </Proxy>
    
        ProxyPass / http://127.0.0.1:3055/
        ProxyPassReverse / https://127.0.0.1:3055/
    
        ProxyPass /api http://127.0.0.1:3055/api
        ProxyPassReverse /api https://127.0.0.1:3055/api
    
        RewriteEngine on
    
        RewriteCond %{HTTP:Upgrade} =websocket [NC]
        RewriteCond %{HTTP:Connection} Upgrade [NC]
        RewriteRule /(.*)           ws://127.0.0.1:3055/$1 [P,L]
        RewriteCond %{HTTP:Upgrade} !=websocket [NC]
        RewriteRule /(.*)           http://127.0.0.1:3055/$1 [P,L]
    
        RewriteCond %{SERVER_NAME} =domain.com [OR]
        RewriteCond %{SERVER_NAME} =www.domain.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    </VirtualHost>
    

    【讨论】:

      【解决方案4】:

      socket_io 的客户端和服务器版本不匹配时收到此错误。尝试查找与服务器正在使用的版本匹配的版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-28
        • 2020-08-17
        • 1970-01-01
        • 2023-03-19
        • 2020-10-10
        • 2022-01-09
        • 2021-09-04
        • 2021-07-19
        相关资源
        最近更新 更多