kataras / neffos.js

Node.js and Browser support for the neffos real-time framework written in Typescript.
MIT License
42 stars 16 forks source link

Reconnect was stopped after first-time retry. #26

Open d2school opened 1 year ago

d2school commented 1 year ago

After first time reconnect (retries = 1), and recived a "http-status 500" (or 503, 404 ...), neffos would no try any more reconnect .

file: /src/neffos.js line : 993:

   let check = (): void => {
        // Note:
        // We do not fire a try immediately after the disconnection as most developers will expect.
        _fetch(endpointHTTP, fetchOptions).then(() => {
            notifyOnline(tries);
        }).catch(() => { // on network failures.
            // if (err !== undefined && err.toString() !== "TypeError: Failed to fetch") {
            //     console.log(err);
            // }
            tries++;
            setTimeout(() => {
                check();
            }, checkEvery)
        });
    };

This would not catch any bad http-status. the new code :

   let check = (): void => {
        // Note:
        // We do not fire a try immediately after the disconnection as most developers will expect.
        _fetch(endpointHTTP, fetchOptions).then((response) => {  // <--- on response
             if (response.ok) {   // on response ok 
                 notifyOnline(tries);
            } else {
                 return Promise.reject(`${response.status}:${response.statusText}`) // <-- reject 
            }
        }).catch((err) => { // on network failures.
            // if (err !== undefined && err.toString() !== "TypeError: Failed to fetch") {
            //     console.log(err);
            // }
            tries++;
            setTimeout(() => {
                check();
            }, checkEvery)
        });
    };

Now, even if the ws-server is restarted after a long time, the ws-client (mneffos.js) can reconnect normally.