arrow_back How to make a global array with multi-thread access in PHP?

1 vote
Good afternoon.
I am currently developing a web server in PHP 7.4.11 that uses sockets and the HTTP protocol to receive messages from the Android client.
I use Workerman library to handle sockets and requests from Android client and the site runs on Apache 2.4.

When you receive a message from Android, you need to send messages via websockets to clients on the site.
There is a file global_vars.php with the following content:

namespace global_vars;

class Globals {
private static $connections = array();

public static function add($connection) { Globals::$connections[$connection->id] = $connection; }
public static function remove($connection) { unset(Globals::$connections[$connection->id]); }
public static function get($id) { return Globals::$connections[$id]; }

public static function keys() { return array_keys(Globals::$connections); }
When I connect a new socket, I add it to the connection array:
include_once "global_vars.php";
I expect to be able to get the current websockets when I receive a request from the Android client, which is handled in the apiServer.php file:
include_once "global_vars.php";
global_vars\Globals::get($target)->send("Message from android");
However, the global_vars\Globals::$connections array from the global_vars.php file appears to be empty. How do I solve the array problem? Or do I need to use a different server architecture to solve my problem? Additionally, I checked with Globals incrementing the counter, and it turned out to be unique for both threads (wsServer.php and apiServer.php), which is quite strange, because it was static (static public $i = 0).

2 Answers

1 vote
Try redis. It might work for you


RigelGL ,
Try playing with this
and preferably post the apache config
Vladimir Korotenko , not only are the questions stupid, but so is the answer.
Look, you don't know any RNR or web servers at all. The other day I asked you to explain on your fingers how a web server works. Why try to answer questions you don't understand?

The main thing the man explains to you, he has objects that hold the network connection, and you still continue to be stupid. WHAT REDDIT ARE YOU GOING TO PUT THEM IN? HOW ARE YOU GOING TO SERIALIZE THE NETWORK CONNECTION?
They explain to you that there is a separate peppy that keeps the connection to the client, and you're still talking about the apache config
Vladimir Korotenko , as an Apache module.
PHPIniDir "D:/Server/bin/php"
AddHandler application/x-httpd-php .php
LoadModule php7_module "D:/Server/bin/php/php7apache2_4.dll"
RigelGL , Again a silly question do you have PHP as an Apache module or as a cgi fcgi ?
RigelGL How heavy are the objects? Is it possible to serialize them quickly? Are native descriptors used there. Something like sockets or files?
Vladimir Korotenko When I try to serialize, I get an error: Exception: Serialization of 'Closure' is not allowed. Serialization is definitely not what I need.
At the moment I am using Redis to get $target, which is the id of the websocket to send the message to.
Redis does not store PHP objects, which is $connection. I need to get exactly the $connection object (to send a message to it), which is placed in global_vars/Globals::connections when websocket is connected.
1 vote
Since you started working with workerman, you should have read the documentation how and what to do there, and go to the git and read the developer's answers to questions, there is a lot of useful information. Alas your implementation is not right, if you run Workerman in several threads you will have problems to find connection, and to send data to socket you need in each process to open TCP connection, that will be used to send messages, it is also in their documentation.