Do not let connected reconnect, PHP

0

I'm working on a WebSockets server that manages connections but I can not solve this small impasse, not for lack of understanding of the code but for not knowing how to do it.

Look,

  • The user connects to the server

  • When another user connects to the server (or at least two are connected) they will connect by exchanging information through the server.

  • When the third connect the same process has to be done, but the first two do not need to reconnect with each other again, they just need to connect with 3 and vice versa .. for all to be connected ..

  • and so on .. up to a maximum of 20 connections.

    In my current server it only receives the message and passes it to all connected less to the one that is sending the message, and this causes the third one to connect with the first one but not with the second one becomes a mess ...

    It's worth noting that the server only passes the messages, who actually treats them and makes the connection is the user in the Browser .. The server is only the signal tower

    How to do this administration in PHP or at least the logic for me to try to do it alone ..

        
    asked by anonymous 18.07.2015 / 17:10

    1 answer

    1
      

    When the third connect the same process has to be done, but the first two do not need to reconnect with each other again, they just need to connect with the 3 and vice versa .. so everyone is connected ..

    As far as I understand the websocket does not connect one to one of the users and yes and all connect to the socket and therefore you do not need to connect 1 with 2 and 2 with 3 and 3 with 1 (that's what I understood from your logic) , so I think the problem is with your script.

    In other words, everyone gets the same data, so isolating the data would be more complicated.

    As a websocket, you will have to use a session cookie system (perhaps created by you) to prevent the user from connecting again, or use an IP scheme (note that more than one computer on a network shares an IP of ISP, then using IPs is a bad practice).

    Verify that the user is already online

    Based on these answers: link

    To bring the user data as the "header cookie" (or token) you will need to use socket_recvfrom

    Verify number of connections

    To prevent more than 20 connections, use socket_accept by adding to a vector, something like:

    $accepts = array();
    
    //Pedaço de código...
    
    $accepts[] = socket_accept(...);
    
    
    //Crie um método que verifica cookies e remove/elimina conexões sem sessão ou com sessão invalida e repetidas
    
    if (count($accepts) > 20) {
       //Se a nova conexão for maior que 20 envia uma resposta que bloqueie o uso do chat no front-end
    }
    

    Something you can use to get started with your project is link

    From my point of view it's a bit outdated, but it's just for study.

        
    18.07.2015 / 21:59