python falsk socket.io.js 无法连接/错误提示 WebSocket connection to ‘ws://xxxxx‘ failed:

问题描述
解决使用python falsk socketio部署后报错WebSocket connection to ‘ws://xxxxx‘ failed:

解决方法
检查自己的页面使用的是http还是https,或者是ws/wss,我使用的是http。前端socket.io.js文件第1568行有个函数var uri = this.uri(),默认获取的值是:ws://xxx.cn/socket.io/?EIO=4&transport=websocket&sid=P8tPESz-mqXU6QDKAAAK,如果使用的是http请求,把ws:替换成http就可以了。

socket.io.js第1568行

socket.io.js修改后(把ws://xxx.cn替换成http://xxx.cn)

       var uri = this.uri();
       
       //把ws:修改为http:
       if (uri){
    
    
          uri = uri.replace('ws:', 'http:')
        }

        var protocols = this.opts.protocols; // React Native only supports the 'headers' option, and will print a warning if anything else is passed

        var opts = isReactNative ? {
    
    } : pick(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");

        if (this.opts.extraHeaders) {
    
    
          opts.headers = this.opts.extraHeaders;
        }

        try {
    
    
          this.ws = usingBrowserWebSocket && !isReactNative ? protocols ? new WebSocket(uri, protocols) : new WebSocket(uri) : new WebSocket(uri, protocols, opts);
        } catch (err) {
    
    
          return this.emitReserved("error", err);
        }

socket.io.js修改前

       	var uri = this.uri();
        
        var protocols = this.opts.protocols; // React Native only supports the 'headers' option, and will print a warning if anything else is passed

        var opts = isReactNative ? {
    
    } : pick(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");

        if (this.opts.extraHeaders) {
    
    
          opts.headers = this.opts.extraHeaders;
        }

        try {
    
    
          this.ws = usingBrowserWebSocket && !isReactNative ? protocols ? new WebSocket(uri, protocols) : new WebSocket(uri) : new WebSocket(uri, protocols, opts);
        } catch (err) {
    
    
          return this.emitReserved("error", err);
        }

猜你喜欢

转载自blog.csdn.net/weixin_42597632/article/details/129157344