Problem description
Solve the error WebSocket connection to 'ws://xxxxx' failed after deploying with python falsk socketio:
Solution
Check whether your page uses http or https, or ws/wss, I use http. There is a function var uri = this.uri() in line 1568 of the front-end socket.io.js file, and the default value obtained is: ws://xxx.cn/socket.io/?EIO=4&transport=websocket&sid=P8tPESz-mqXU6QDKAAAK , if you are using http request, just replace ws: with http.
After modifying socket.io.js (replace ws://xxx.cn with 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 before modification
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);
}