Skip to content

The Socket Io server drops if the tab is inactive #3315

@wistis

Description

@wistis

There is a chat. Socket Io server starts and works, if a client connects to it, and then switched to another tab, seconds to 25-30, then after a while if you go back to the tab with the chat, the messages are not sent, if the page is updated it goes when you connect to server scripts we get 504 Gateway Time-out. The server itself works on the source port, but does not display the information. At the time of this connection, Apach consumes up to 100% of CPU.
`var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var fs = require('fs');
var https = require('https');

server.listen(5000, function () {
console.log('Server listening at port %d', 5000);
});
const opts = {
key: fs.readFileSync('/var/www/httpd-cert/olever/site.key'),
cert: fs.readFileSync('/var/www/httpd-cert/olever/site.crt')
}

var httpsServer = https.createServer(opts, app);
httpsServer.listen(5001, function(){
console.log("HTTPS on port1 " + 5001);
})

io.attach(httpsServer);
io.attach(server);

io.on('connection', (socket) => {
console.log('a user connected');
socket.on('click', function(data) {
console.log(JSON.stringify(data)+'11');

    socket.broadcast.emit('new message', {
        username: socket.username,
        message: data
    });

});


let token = socket.handshake.query.token;
let admin = socket.handshake.query.admin;
    console.log(token);
    socket.join(token);

socket.on(token+'_write', function(msg) {
    console.log( msg['message']);
    if (admin == 1) {

        socket.broadcast.emit(token + '_write', {
            username: token,
            message: msg['message']
        });

    }
});

socket.on('disconnect', function() {
    console.log('disconnect');
});

socket.on(token, function(msg){

 if(admin==1) {
     console.log(msg['mess']);
     socket.broadcast.emit(token, {
         username: token,
         message: msg
     });

 }
    console.log('message: ' + msg);

if(admin==0) {
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

var body = 'my_data=' + encodeURIComponent(JSON.stringify(msg));


xhr.open("POST", '/send_chat', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(body);

}
});
});`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions