简体   繁体   中英

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

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

I want to be able to keep track of who is connected, and keep a total of certain data. For example if each individual client has 0.5 balance, I want them to be able to know that the total is 3 balance, because of the server.

The most obvious approach I can think of is to do AJAX couple with calls to MySQL to keep track of things, but that seems long-winded.

I could also do CURL but that would be technically challenging.

Is there a simple and straight forward way of simply totaling a balance from six different socket.io clients and sending the information back to them?

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

Bear in mind that you will have to implement some multiprocessing 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);

Another can then access it just like this:

echo apc_fetch("IncrementValue");

It is a fast although crude way to communicate between sessions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM