curveball / core

The Curveball framework is a TypeScript framework for node.js with support for modern HTTP features.
https://curveballjs.org/
MIT License
525 stars 7 forks source link

WebSockets on the same port as HTTP [Firefox/Chrome] #159

Closed spidgorny closed 2 years ago

spidgorny commented 3 years ago

AFAIK it's possible to have WebSockets server on the same port as the normal HTTP server. However when I tried this I got mixed results.

Firefox: shows the correct '/' controller. image

Chrome: shows "Upgrade Required" with HTTP status 406. image

        const app = new Application();
        app.use(router('/', new IndexController(this.redis)));

        let port = parseInt(process.env.PORT!, 10);
        const server = http.createServer(app.callback());
        server.on('upgrade', app.upgradeCallback.bind(this));
        server.listen(port, '0.0.0.0');

        const serverWs = app.listenWs(port);
        this.socketController = new SocketController(serverWs);
        app.use(router('/socket', this.socketController));

I'm confused. Why would two browsers behave different? Anybody seen this before?

evert commented 3 years ago

Very weird, I don't know why this is happening :/

The intent is 100% to only need one listen call and get the websocket for free, but I didn't have the time to figure out how to do this elegantly. listenWs was really just a temporary measure to get the feature going until I was able to do this the right way.