swoole的worker程序變數賦值
阿新 • • 發佈:2018-11-05
<?php Class Test{ public $index = 18; } Class Server { private $server; public $test; public function __construct() { $this->server = new swoole_server('0.0.0.0', 9501); //設定swoole_server $this->server->set([ 'worker_num' => 8, 'max_request' => 10000, 'deamonize' => false, 'task_worker_num' => 8, ]); $this->server->on('Start', [$this, 'onStart']); $this->server->on('Connect', [$this, 'onConnect']); $this->server->on('Receive', [$this, 'onReceive']); $this->server->on('Close', [$this, 'onClose']); $this->server->on('Task', [$this, 'onTask']); $this->server->on('Finish', [$this, 'onFinish']); $this->server->start(); } public function onStart($server) { echo "Start\n"; } //返回資料 在建立連線 $fd描述符 public function onConnect($server, $fd, $from_id) { $server->send($fd, "建立連線 {$fd}"); } public function onReceive(swoole_server $server, $fd, $from_id, $data) { $data = [ 'task' => 'task_demo', 'params' => $data, 'fd' => $fd ]; $this->test = new Test(); var_dump($this->test); //投遞任務 $server->task(serialize($this->test)); // echo "客戶端傳送過來的訊息是{$fd}:{$data}\n"; // $server->send($fd, $data); } public function onTask($server, $task_id, $from_id, $data) { //$data = json_decode($data, true); $data = unserialize($data); // 這個地方去改變值並不影響全域性 $data->index = 22; var_dump($data); var_dump($this->test); // echo "接受Task:{$data['task']}\n"; // $server->send($data['fd'] , "接受到資料Success"); return "Finished"; } public function onFinish($server, $task_id, $data) { var_dump($this->test); echo "Task {$task_id} finish\n"; echo "完成:{$data}\n"; } public function onClose($server, $fd, $from_id) { echo "關閉{$fd}客戶端請求"; } } //為同步阻塞 $server = new Server();
[email protected]:/var/www/code_workspace/socket# php server.php Start [2018-10-15 03:18:05 *32.0] WARNING sw_coro_create: xdebug do not support coroutine, please notice that it lead to coredump. [2018-10-15 03:18:16 *32.0] WARNING sw_coro_create: xdebug do not support coroutine, please notice that it lead to coredump. /var/www/code_workspace/socket/server.php:50: class Test#6 (1) { public $index => int(18) } [2018-10-15 03:18:16 *32.0] WARNING sw_coro_create: xdebug do not support coroutine, please notice that it lead to coredump. 關閉1客戶端請求/var/www/code_workspace/socket/server.php:66: class Test#6 (1) { public $index => int(22) } /var/www/code_workspace/socket/server.php:68: NULL /var/www/code_workspace/socket/server.php:77: class Test#6 (1) { public $index => int(18) } Task 0 finish 完成:Finished