R
R
Roman Andreevich2020-01-09 15:05:31
Node.js
Roman Andreevich, 2020-01-09 15:05:31

Why can ws client behave like this?

Colleagues, good time of the day, I ask for help or advice.
In general, the essence is this, there is a server, there is a client, which I actually write in NodeJS. I run everything locally on ubuntu 18.04 via WebStorm. Actually, everything is fine, the application started, connected via ws to the server, messages went back and forth, but there is a moment)) I leave the computer for a while, and after this very time, a disconnect occurs on the server with my client, with the close or error event ws fails, i.e. the client seems to be connected and the server does not have it.
Has anyone else been in a similar situation, what could be the issue?
Just in case, the client code (well, you never know):

'use strict';
const WebSocket = require('ws');

const logger = require('../logger');
const event = require('../eventBus');

module.exports = class WebSockets {

    constructor() {

        this.url ='wss://test.ru/ws';
        this.client = null;
        this.timeout = 0;
        this.time = 1000 * 10;

        event.bus.on('ws:connect', this.connect.bind(this));
        event.bus.on('ws:send', this.sendMessage.bind(this));

    }

    connect() {

        this.client = new WebSocket(this.url, '',{});

        this.client.addEventListener('open', this._openHandler.bind(this));
        this.client.addEventListener('message', this._messageHandler.bind(this));
        this.client.addEventListener('error', this._errorHandler.bind(this));
        this.client.addEventListener('close', this._reconnection.bind(this));

    }

    _openHandler() {

        if (this.timeout !== 0) {

            clearTimeout(this.timeout);
            this.timeout = 0;

        }
        logger(`info`, `[ws][-- open connection --]`);

    }

    _messageHandler(message) {

        let data = (message.data) ? JSON.parse(message.data) : [];

        if (data.length > 0) event.bus.emit(`app:command`, data);

    }

    _errorHandler() {
        logger(`error`, `[ws][-- error connection --]`);
    }

    _reconnection() {

        this.timeout = setTimeout(() => {

            logger(`error`, `[ws][-- reconnecting ... --]`);
            this.connect();

        }, this.time);

    }

    _close() {

        logger(`error`, `[websockets][close][ -- close connection -- ]`);

        // this.client.removeEventListener('open', this._openHandler.bind(this));
        // this.client.removeEventListener('message', this._messageHandler.bind(this));
        // this.client.removeEventListener('error', this._errorHandler.bind(this));
        // this.client.removeEventListener('close', this._reconnection.bind(this));

        // this.client.close(1000, `dead`);

    }

    sendMessage(object) {

        (this.client !== null)
            ? this.client.send(JSON.stringify(object))
            : logger(`error`, `[websockets][sending][ -- no connection -- ]`);

    }

};

It seems like there is nothing overly complicated, but nevertheless. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-01-09
@Nc_Soft

If the server has proxying from nginx to ws, then there may be a parameter
proxy_read_timeout
Default:
proxy_read_timeout 60s;
Context: http, server, location
https://nginx.org/en/docs/http/ngx_http_proxy_modu... it
is also possible that there is some kind of ping/pong checker on the server, but you do not process it.
https://github.com/websockets/ws#how-to-detect-and...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question