pcntl_fork()
function. It creates a new process (child), which is almost a complete copy of the parent process that performs this call.pcntl_fork()
algorithm forks: in case of successful execution of the function pcntl_fork()
it returns the PID of the child process to the parent, and NULL to the child. If the creation of the fork fails, the pcntl_fork()
function returns a value of −1). $pid = pcntl_fork(); // // if ($pid == -1) { // } elseif ($pid) { // } else { // , PID getmypid() }
$childs = array(); for ($i=0; $i<4; $i++) { $pid = pcntl_fork(); // if ($pid == -1) { die("error: pcntl_fork"); } elseif ($pid) { // $childs[] = $pid; // PID, :) } else { // break; // , } }
stream_socket_pair()
function creates a pair of related indistinguishable stream sockets. Thus, we can write to one socket, and read data from the second. $pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); // fwrite($pair[0], ''); // fread($pair[1], mb_strlen('')); //
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); // $pid = pcntl_fork(); // // if ($pid == -1) { die("error: pcntl_fork"); } elseif ($pid) { // fclose($pair[0]); // $child = $pair[1]; // } else { // fclose($pair[1]); // $parent = $pair[0]; // }
$parent = null; $childs = array(); for ($i=0; $i<5; $i++) { $pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); // $pid = pcntl_fork(); // if ($pid == -1) { die("error: pcntl_fork"); } elseif ($pid) { // fclose($pair[0]); // $childs[] = $pair[1]; // } else { // fclose($pair[1]); // $parent = $pair[0]; // break; // , } }
$childs
array will contain all the sockets to communicate with the children, and the descendants will use $parent
to communicate with the $parent
. $service = stream_socket_server('unix:///tmp/websocket.sock', $errorNumber, $errorString);
limit_conn_zone $binary_remote_addr zone=perip:10m; server { listen 5.135.163.218:80; server_name sharoid.ru; location / { limit_conn perip 5; # 5 1 ip proxy_pass http://127.0.0.1:8000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 3600s; # } }
php websocket.php
command php websocket.php
or ./websocket.php
(after giving permission to execute)nohup
, for example, nohup ./websocket.php &
, then the script will continue to work after closing the console.ulimit -n 65535
, and if the user has insufficient privileges, then sudo sh -c "ulimit -n 65535 && exec su $LOGNAME"
. The current value can be viewed using the command ulimit -n
stream_select()
function: it does not accept more than 1024 connections. It is more complicated here - you need to recompile php with an increased FD_SETSIZE
unix:///tmp/websocket.sock
), we can connect to this socket in any place of our website or in the crown and send a message that the master will send all workers, and they, in turn, all customers: $service = stream_socket_client ('unix:///tmp/websocket.sock', $errno, $errstr); fwrite($service, ' ');
Yii::app()->websocket->send(' ');
extensions/websocket
commands
folder we put from WebsocketCommand.php from the sample / yii folder. 'websocket' => array( 'class' => 'Websocket', //'websocket' => 'tcp://127.0.0.1:8000', //'localsocket' => 'tcp://127.0.0.1:8001',// unix:///tmp/mysock //'workers' => 1 ),
'ext.websocket.*'
Source: https://habr.com/ru/post/210228/
All Articles