Comments can be ignored. There may be errors added by individuals. Do not mislead you. . .
server side
<?php class Server { private $serv; public function __construct() { $this->serv = new swoole_server("0.0.0.0", 9501); $this->serv->set(array( 'worker_num' => 8, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 2, 'debug_mode'=> 1, 'task_worker_num' => 8,//Assuming that the task behind 1 is to be queued, if more can be processed at the same time /* The processing time of a single task, such as 100ms, that one process can process 1/0.1=10 tasks in 1 second The speed of task delivery, such as 2000 tasks per second 2000/10=200, need to set task_worker_num => 200, enable 200 task processes */ )); $this->serv->on('Start', array($this, 'onStart')); $this->serv->on('Connect', array($this, 'onConnect')); $this->serv->on('Receive', array($this, 'onReceive')); $this->serv->on('Close', array($this, 'onClose')); // bind callback $this->serv->on('Task', array($this, 'onTask')); $this->serv->on('Finish', array($this, 'onFinish')); $this->serv->start(); } public function onStart( $serv ) { echo "Start\n"; } public function onConnect( $serv, $fd, $from_id ) { echo "Client {$fd} connect\n"; } public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { echo "Get Message From Client {$fd}:{$data}\n"; // send a task to task worker. $param = array( 'fd' => $fd ); $serv->task( json_encode( $param ) ); echo "Continue Handle Worker\n"; } public function onClose( $serv, $fd, $from_id ) { echo "Client {$fd} close connection\n"; } //function onTask(swoole_server $serv, int $task_id, int $src_worker_id, mixed $data); public function onTask($serv,$task_id,$from_id, $data) { echo "This Task {$task_id} from Worker {$from_id}\n"; echo "Data: {$data}\n"; for($i = 0 ; $i < 10 ; $i ++ ) { sleep(1); echo "Taks {$task_id} Handle {$i} times...\n"; } $fd = json_decode( $data , true )['fd']; $serv->send( $fd , "Data in Task {$task_id}"); return "Task {$task_id}'s result"; } public function onFinish($serv,$task_id, $data) { echo "Task {$task_id} finish\n"; echo "Result: {$data}\n"; } } $server = new Server(); /* Continue Handle Worker This Task 0 from Worker 7 Data: {"fd":1} Taks 0 Handle 0 times... Taks 0 Handle 1 times... Taks 0 Handle 2 times... Taks 0 Handle 3 times... Taks 0 Handle 4 times... Taks 0 Handle 5 times... Taks 0 Handle 6 times... Taks 0 Handle 7 times... Taks 0 Handle 8 times... Taks 0 Handle 9 times... Task 0 finish Result: Task 0's result */ ?>
client:
<?php class Client { private $client; public function __construct() { $this->client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $this->client->on('Connect', array($this, 'onConnect')); $this->client->on('Receive', array($this, 'onReceive')); $this->client->on('Close', array($this, 'onClose')); $this->client->on('Error', array($this, 'onError')); } public function connect() { $fp = $this->client->connect("127.0.0.1", 9501 , 1); if( !$fp ) { echo "Error: {$fp->errMsg}[{$fp->errCode}]\n"; return; } } public function onReceive( $cli, $data ) { echo "Get Message From Server: {$data}\n"; } public function onConnect( $cli) { fwrite(STDOUT, "Enter Msg:");//Output to the terminal //bool swoole_event_add(mixed $sock, mixed $read_callback, mixed $write_callback = null,int $flags = null); /* The swoole_event_add function is used to add a socket to the underlying reactor event listener. This function can be used in Server or Client mode. Function prototype: */ swoole_event_add(STDIN, function($fp){//Input assumes that the bottom layer is io multiplexing listening input descriptor global $cli;//Set the global or it will report an error fwrite(STDOUT, "Enter Msg:"); $msg = trim(fgets(STDIN)); //remove input spaces $cli->send( $msg );// Then send the message. . . }); } // public function onClose( $cli) { echo "Client close connection\n"; } public function onError() { } public function send($data) { $this->client->send( $data ); } public function isConnected() { return $this->client->isConnected(); } } $cli = new Client(); $cli->connect();