0

I have client socket.io js and backend phpsocket.io, I can do it on a server without a domain, making use of the IP nothing else and it works for me, it sends and receives information, but when I mount it on a server with a domain, it never send nothing. I have enabled TCP port 2300, and I use AWS as a server. I have done many tests in production with a server without domain and without certificate and everything goes well, but it is when it has the certificate it fails.

Client code where I run the js to connect from local to the server.

<script type="text/javascript">

    const idusuario= "<?=$this->idusuario?>";

    const socket = io.connect('https://myserver.muestra.com:2300', {secure: true});
    console.log(idusuario);
    socket.on('connect', () => {
        socket.emit('add user', idusuario);
    });

    $('.msg1').click(function (){
        console.log('clickeado 1++**********');
        console.log(idusuario);
        socket.emit('add user', idusuario);
    });



    $('.msg2').click(function (){
        console.log('clickeado 2');
        socket.emit('chat message', '<?=session_id()?> HOLA');
    });

    $('.msg3').click(function (){
        console.log('clickeado 3');
        socket.emit('allusers');
    });

    $('.msg4').click(function (){
        console.log('clickeado 4');
        socket.emit('allusersobject');
    });

    socket.on('chat message', data => {
        console.log(data)
    });
    socket.on('user joined', data => {
        console.log(data)
        /*console.log(JSON.parse(data));*/
    });
    socket.on('user left', data => {
        console.log(data);
        console.log(idusuario);
        console.log(data.idusuario);
        var usuarioeliminado = JSON.parse(data.idusuario)
        if (idusuario===usuarioeliminado.idusuario) {
            console.log('SON IGUALES');
            socket.emit('add user', $('.iduser').val());
        }
    });

    socket.on('allusers', data => {
        console.log(data);
    });

    socket.on('allusersobject', data => {
        console.log(JSON.parse(data.numUsers))
        console.log(JSON.parse(data.users))
    });




</script>

I also have this in the backend, which I use to get the queries running and where I keep the users.

require_once 'config/config.php';
require_once 'vendor/autoload.php';

$io = new SocketIO(2300);

$io->on('connection', function ($socket) {
$socket->addedUser = false;
// when the client emits 'new message', this listens and executes
$socket->on('new message', function ($data) use ($socket) {
    // we tell the client to execute 'new message'
    $socket->broadcast->emit('new message', array(
        'username' => $socket->username,
        'message' => $data
    ));
});

// when the client emits 'add user', this listens and executes
$socket->on('add user', function ($username) use ($socket) {
    global $usernames, $numUsers;


    // remove the username from global usernames list

    if (isset($usernames[$username])):
        unset($usernames[$username]);
        --$numUsers;
    endif;


    $conexion = new Conexion();
    $sql = "select idusuario,nickname from usuarios where idusuario = $username";
    $usuario = $conexion->getdato($sql);


     
    $socket->username = $usuario;
     
    $usernames[$username] = $username;
    ++$numUsers;
    $socket->addedUser = true;
    $socket->emit('login', array(
        'numUsers' => $numUsers
    ));
    // echo globally (all clients) that a person has connected
    $socket->broadcast->emit('user joined', array(
        'username' => json_encode($usernames),
        'numUsers' => $numUsers
    ));
});

 
$socket->on('typing', function () use ($socket) {
    $socket->broadcast->emit('typing', array(
        'username' => $socket->username
    ));
});

 
$socket->on('stop typing', function () use ($socket) {
    $socket->broadcast->emit('stop typing', array(
        'username' => $socket->username
    ));
});
$socket->on('allusers', function () use ($socket) {
    global $usernames, $numUsers;
    
    $socket->broadcast->emit('allusers', array(
        'users' => json_encode($usernames),
        'numUsers' => $numUsers
    ));
});


$socket->on('allusersobject', function () use ($socket) {
    global $usernames, $numUsers;
     
    $conexion = new Conexion();
    $users = [];
    foreach ($usernames as $item) {

        $sql = "select idusuario,nickname,idroll from usuarios where idusuario = $item";
        $users[] = $conexion->getdato($sql);
    }
    $socket->broadcast->emit('allusersobject', array(
        'users' => json_encode($users),
        'numUsers' => $numUsers
    ));
});


 
$socket->on('disconnect', function () use ($socket) {
    global $usernames, $numUsers;
           

    if ($socket->addedUser) {
        unset($usernames[$socket->username->idusuario]);
        --$numUsers;

        // echo globally that this client has left
        $socket->broadcast->emit('user left', array(
            'username' => $socket->username,
            'numUsers' => $numUsers,
            'idusuario' => json_encode(['idusuario'=>$socket->username->idusuario])
        ));
    }




   });

  });

  if (!defined('GLOBAL_START')) {
   Worker::runAll();
  }

Everything indicates that it would be something related to the certificate or cloudflare.

2
  • Is it a self signed certificate? Often you find issues with non-authoritative sourced certificates.
    – hppycoder
    Mar 16, 2021 at 17:26
  • it is not a self-signed certificate. Mar 17, 2021 at 17:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.