in

socket.io WebSocket connection failed


I am using socket.io to connect to a different domain, and it can successfully connect using polling, however when attempting to connect using websockets gets the error “WebSocket connection to ‘wss://XXXXX’ failed”.

After observing the network activity the server seems to indicate that it is capable of upgrading to a websocket connection (although I won’t claim to be an expert in understanding these requests), but isn’t able to do so:

console shows that its not able to make websocket connection

I’m just trying to produce a minimal viable product right now so here is my node.js server code:

let http = require('http');
let https = require('https');
let fs = require('fs'); 
let express = require('express');

const privateKey = fs.readFileSync('XXXXXXXXX', 'utf8');
const certificate = fs.readFileSync('XXXXXXXXX', 'utf8');
const ca = fs.readFileSync('XXXXXXXXX', 'utf8');

const options = {
    key: privateKey,
    cert: certificate,
    ca: ca
};

let app = express();

let httpsServer = https.createServer(options,app);
let io = require('socket.io')(httpsServer, {
    cors: {
        origin: true
    }
});
httpsServer.listen(443);

console.log('starting');

io.sockets.on('connection', (socket) => {
    console.log("something is happening right now")

    socket.on("salutations", data => {
        console.log(`you are now connected via ${socket.conn.transport.name}`);
        socket.emit("greetings", "I am the socket confirming that we are now connected");
    });
});

Client-side JavaScript:

const socket = io("https://XXXXXXX");
console.log(socket);

socket.on("connect", () => {
    console.log("now connected");

    socket.on("message", data => {
        console.log(data);
    });

    socket.on("greetings", (elem) => {
        console.log(elem);
    });
});

let h1 = document.querySelector('h1');
h1.addEventListener('click',()=>{
    console.log("I am now doing something");
    socket.emit("salutations", "Hello!");
})

The only suggestion in the official documentation for this issue isn’t relevant because I’m not using a proxy, and other suggested fixes result in no connection at all (presumably because they prevent it from falling back to polling)



Source: https://stackoverflow.com/questions/70633932/socket-io-websocket-connection-failed

a neural network which can simulate a lava lamp in an infinite loop

Let’s See How Far iPhone Has Come Since Its Launch