OpenHausIO / backend

HTTP API for the OpenHaus SmartHome/IoT solution
https://docs.open-haus.io
6 stars 2 forks source link

Commonly used interface for http & ws breaks connections #458

Closed mStirner closed 6 months ago

mStirner commented 6 months ago

Alle Jahre wieder...

When a interface connection is used for http and alter for https, its break the websocket connection. "RSV ... frame must be clear". This is due to the "keep-alive" handling in the iface.httpAgent():

https://github.com/OpenHausIO/backend/blob/d17066bd3bd43878371d2fa618651068c95ed78c/components/devices/class.interface.js#L285-L287

The agent is created, cached, and then the agent from the http request is returned: https://github.com/OpenHausIO/backend/blob/d17066bd3bd43878371d2fa618651068c95ed78c/components/devices/class.interface.js#L210-L212

There needs to be a way to check the URL for which the connection is used.


    iface.on("attached", () => {

        console.log("iface attacehd");
        let agent = iface.httpAgent();
        request(`http://${host}:${port}/socket.io/1/?t=${Date.now()}`, {
            agent
        }, (err, result) => {
            if (err) {

                console.error(err);
                process.exit(1);

            } else {

                let start = null;
                let wsp = result.body.toString().split(":")[0];

                console.log("WS GET", wsp)
                let ws = new WebSocket(`ws://${host}:${port}/socket.io/1/websocket/${wsp}`, {
                    agent
                });

Cause the trouble described above. Add a option to the agent to unpipe everything from the upstream & re-create a new uncached agent?

mStirner commented 6 months ago

[!TIP] Workaround: Create 2 interfaces with the same host/port settings. Use one dedicated for HTTP and one for WebSocket connections.

mStirner commented 6 months ago

Use new interface method .bridge() to create new sockets. In v4 the iface.httpAgent(),iface.httpsAgent() uses this under the hood and no action/workaround is needed anymore.

See https://github.com/mStirner/backend/commit/fe8c2a94b920d7a09d62e65aa956ad13e1c9b4b2