1 <?php 2 ini_set('default_socket_timeout', -1); 3 class serverEmail 4 { 5 public $serv = null; 6 7 public function __construct() 8 { 9 $this->serv = new swoole_server('0.0.0.0', 9508, SWOOLE_BASE); 10 $this->serv->set([ 11 'worker_num' => 1, 12 'task_worker_num' => 1, 13 'dispatch_mode' => 2, 14 'daemonize' => 0, 15 //'debug_mode'=> 0, 16 ]); 17 18 $this->serv->on('receive',[$this,'onReceive']); 19 //$this->serv->on('receive', function ($serv, $fd, $from_id, $data) { 20 // $serv->send($fd, "Server: ".$data); 21 //}); 22 23 $this->serv->on('connect',[$this,'onConnect']); 24 $this->serv->on('workerstart', [$this, 'onWorkerStart']); 25 $this->serv->on('task', [$this, 'onTask']); 26 $this->serv->on('finish', [$this, 'onFinish']); 27 $this->serv->on('start', [$this, 'onStart']); 28 $this->serv->on('message', [$this, 'onMessage']); 29 $this->serv->on('close', [$this, 'onClose']); 30 31 //$this->serv->on('request', function ($request, $response) {echo 'xxxx';}); 32 $this->serv->start(); 33 } 34 35 36 public function onReceive(swoole_server $serv, $fd, $from_id, $data) 37 { 38 $serv->send($fd, "Server: ".$data); 39 // $client->subscribe('sendmail'); 40 echo "this is receive\n"; 41 $serv->task($data); 42 43 } 44 public function onTask(swoole_server $serv, $task_id, $from_id, $data) 45 { 46 echo "this is task\n"; 47 return 'xxx'; 48 } 49 50 public function onFinish(swoole_server $serv, $task_id, $msg) 51 { 52 echo "this is finish\n"; 53 return 'xx'; 54 } 55 56 public function onClose(swoole_server $serv, $fd) 57 { 58 echo "Client {$fd} close connection\n"; 59 } 60 61 62 public function onWorkerStart(swoole_server $serv, $task_id) 63 { 64 echo 'this is workStart'.$this->serv->worker_id.'-'.$task_id."\n"; 65 if($this->serv->worker_id == 1){ 66 $redis = new Redis(); 67 $redis->pconnect('10.12.8.242', 6379); 68 //$redis->auth('123qwe!!'); 69 $redis->subscribe(array('swoole_test'), function($instance, $channelName, $result) { 70 $this->serv->task($result); 71 }); 72 } 73 } 74 75 public function onMessage(swoole_server $serv, $result) 76 { 77 echo "this is message\n"; 78 print_r($result); 79 } 80 81 public function onConnect( swoole_server $serv, $fd, $from_id) 82 { 83 echo "this is connect\n"; 84 } 85 86 public function onStart() 87 { 88 echo "this is start\n"; 89 } 90 91 92 } 93 $server = new serverEmail();
//ini_set('default_socket_timeout', -1);
间隔1分钟会报redis异常,信息如下:问题总结出php redis客户端是socket协议通信的。
#3 /opt/aspire/product/tst_cmdn/apache/htdocs/sendmail/swoole_xx.php(93): serverEmail->__construct()
#4 {main}
thrown in /opt/aspire/product/tst_cmdn/apache/htdocs/sendmail/swoole_xx.php on line 71
[2018-10-17 20:27:05 *15915.1] ERROR zm_deactivate_swoole (ERROR 503): Fatal error: Uncaught exception 'RedisException' with message 'read error on connection' in /opt/aspire/product/tst_cmdn/apache/htdocs/sendmail/swoole_xx.php:71
Stack trace:
#0 /opt/aspire/product/tst_cmdn/apache/htdocs/sendmail/swoole_xx.php(71): Redis->subscribe(Array, Object(Closure))
#1 [internal function]: serverEmail->onWorkerStart(Object(swoole_server), 1)
#2 /opt/aspire/product/tst_cmdn/apache/htdocs/sendmail/swoole_xx.php(32): swoole_server->start()
#3 /opt/aspire
[/opt/aspire/product/tst_cmdn/tmp/swoole-src-1.9.8/src/network/ProcessPool.c:415@swProcessPool_wait][Manager] worker stop.pid=15915
this is workStart1-1
swoole_server的两种运行模式启动进程区别
具体参照swoole官网文档:SWOOLE_BASE模式下Reactor
和Worker
是同一个角色
swoole_websocket_server 继承自 swoole_http_server,swoole_http_server 继承自 swoole_server,所有swoole_server的方法,都可以用,Onmessage方法是swoole_websocket_server是用到的。