Don’t let connected connect, PHP

Asked

Viewed 75 times

0

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

Look at that,

  1. The user connects to the server

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

  3. When the third one connects the same process has to be done, but the first two don’t need to re-connect to each other again, they just need to connect to 3 and vice versa.. for everyone to be connected..

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

On my current server it just receives the message and forwards it to everyone connected except to the one sending the message, and this causes the third to connect with the first but not with the second ai becomes a mess...

It is worth noting that the server only forwards the messages, who treats them and make the actual 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 myself..

  • It is a websocket or is a request (ajax for example)?

1 answer

1


When the third one connects the same process has to be done, but the first two don’t need to re-connect to each other again, they just need to connect to 3 and vice versa.. for everyone to be 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 so does 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 believe the problem is with your script.

In other words, all receive the same data, to isolate the data would be more complicated.

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

Check if the user is already online

Based on this answer: /a/60061/3635

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

Check amount of connections

To prevent more than 20 connections, use the socket_accept 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 start your project is the https://github.com/Flynsarmy/PHPWebSocket-Chat

From my point of view it’s a little outdated, but it’s just for study.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.