PHP / C ++ server to handle socket.io clients

There is a website that uses socket.io as an "API". In fact, anyone can download the source code of a website and modify it to make their own client with little difficulty. For example, I have six clients, all of which run the same client side of the script. The script is hosted on my own domain, not on it with socket.io API.

I want to be able to track who is connected and to store certain data in general. For example, if each individual client has 0.5 balance, I want them to know that the total amount is 3 balance due to the server.

The most obvious approach I can come up with is to make an AJAX pair with MySQL calls to keep track of things, but that seems long.

I could also do CURL, but that would be technically difficult.

Is there a simple and direct way to simply summarize the balance from six different socket.io clients and send the information back to them?

+4
source share
1 answer

Use an object caching layer, such as APC or memcached, as shared memory in your PHP server. This way you can maintain synchronization between different instances / clients.

Keep in mind that you will have to implement some multi-processor security, as this is not thread safe.

For instance:

/* PHP application 1: */ (($value = apc_fetch("IncrementValue")) != FALSE) ? apc_store("IncrementValue",$value+1) : apc_store("IncrementValue",1); 

Then you can access it like this:

 echo apc_fetch("IncrementValue"); 

This is a quick, albeit crude way to communicate between sessions.

0
source

Source: https://habr.com/ru/post/1495401/


All Articles