<?php
ini_set('default_socket_timeout', -1);
class serverEmail
{
public $serv = null; public function __construct()
{
$this->serv = new swoole_server('0.0.0.0', 9508, SWOOLE_BASE);
$this->serv->set([
'worker_num' => 1,
'task_worker_num' => 1,
'dispatch_mode' => 2,
'daemonize' => 0,
//'debug_mode'=> 0,
]); $this->serv->on('receive',[$this,'onReceive']);
//$this->serv->on('receive', function ($serv, $fd, $from_id, $data) {
// $serv->send($fd, "Server: ".$data);
//}); $this->serv->on('connect',[$this,'onConnect']);
$this->serv->on('workerstart', [$this, 'onWorkerStart']);
$this->serv->on('task', [$this, 'onTask']);
$this->serv->on('finish', [$this, 'onFinish']);
$this->serv->on('start', [$this, 'onStart']);
$this->serv->on('message', [$this, 'onMessage']);
$this->serv->on('close', [$this, 'onClose']); //$this->serv->on('request', function ($request, $response) {echo 'xxxx';});
$this->serv->start();
} public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
$serv->send($fd, "Server: ".$data);
// $client->subscribe('sendmail');
echo "this is receive\n";
$serv->task($data); }
public function onTask(swoole_server $serv, $task_id, $from_id, $data)
{
echo "this is task\n";
return 'xxx';
} public function onFinish(swoole_server $serv, $task_id, $msg)
{
echo "this is finish\n";
return 'xx';
} public function onClose(swoole_server $serv, $fd)
{
echo "Client {$fd} close connection\n";
} public function onWorkerStart(swoole_server $serv, $task_id)
{
echo 'this is workStart'.$this->serv->worker_id.'-'.$task_id."\n";
if($this->serv->worker_id == 1){
$redis = new Redis();
$redis->pconnect('10.12.8.242', 6379);
//$redis->auth('123qwe!!');
$redis->subscribe(array('swoole_test'), function($instance, $channelName, $result) {
$this->serv->task($result);
});
}
} public function onMessage(swoole_server $serv, $result)
{
echo "this is message\n";
print_r($result);
} public function onConnect( swoole_server $serv, $fd, $from_id)
{
echo "this is connect\n";
} public function onStart()
{
echo "this is start\n";
} }
$server = new serverEmail();

swoole学习-LMLPHP swoole学习-LMLPHP //ini_set('default_socket_timeout', -1); swoole学习-LMLPHP
间隔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学习-LMLPHP swoole学习-LMLPHP 具体参照swoole官网文档:SWOOLE_BASE模式下ReactorWorker是同一个角色
swoole_websocket_server 继承自 swoole_http_server,swoole_http_server 继承自 swoole_server,所有swoole_server的方法,都可以用,Onmessage方法是swoole_websocket_server是用到的。
05-11 17:43