PHP IO socket only connect a few times

0

I have a huge problem, and that is that I am trying to connect to a socket with SocketIO from a client and sometimes it connects as sometimes it does not simply.

It really is very annoying because it is a huge problem that this happens. This is my code

Client-side


window.db.connection = io(db_vars.SOCKET.URL + ":" + db_vars.SOCKET.PORT);

window.db.connection.emit('add user', this.username, function(response) {
     console.log(response);
});

window.dibibot.connection.on('login', function(data) {
     console.log(data);
});

Server-side


$io->on('connection', function($socket)
{
    $socket->loggedUser = false;

    // cuando se ejecute en el cliente el evento add user

    $socket->on('add user', function ($username, $callback) use($socket)
    {
        global $usernames, $numUsers;
        // guardamos al usuario en sesión
        $socket->username = json_decode($username, true);
        $socket->username["SOCKET_ID"] = $socket->id;

        //añadimos al cliente a la lista global
        $usernames[$socket->username["WP_USER_DATA"]['guid']] = $socket->username;
        ++$numUsers;

        $socket->loggedUser = true;

        $socket->emit('login', array( 
            'numUsers' => $numUsers,
            'usernames' => $usernames
        ));

        call_user_func($callback, "ok");

        foreach ($usernames as $key => $value) {
            if($value["DIBIBOT_USER_ROLE"] == "dibibot_operator") {
                $socket->to($value["SOCKET_ID"])->emit('user joined', array(
                    'username' => $socket->username,
                    'numUsers' => $numUsers,
                    "usernames"=> $usernames
                ));
            }
        } 
    });
});

What could be happening, as I say, sometimes it connects and sometimes just does not do it and I do not know what could be happening, some IO socket configuration maybe?

    
asked by Anthony Medina 23.08.2018 в 16:25
source

0 answers