<?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();

//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模式下ReactorWorker是同一个角色
swoole_websocket_server 继承自 swoole_http_server,swoole_http_server 继承自 swoole_server,所有swoole_server的方法,都可以用,Onmessage方法是swoole_websocket_server是用到的。

swoole学习的更多相关文章

  1. swoole学习(二)----搭建server和client

    1.搭建server 1.1搭建server.php 1.搭建websocket服务器,首先建立 server.php 文件, <?php $server = new swoole_websoc ...

  2. swoole学习(一)----linux安装swoole

    1.下载swoole 登录swoole官网 https://www.swoole.com/ 点击下载,找到github或者其他链接下载下来 放到虚拟机上. 也可以使用虚拟机下载 2.登录虚拟机 推荐使 ...

  3. Swoole学习(七)Swoole之异步TCP服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...

  4. Swoole学习(五)Swoole之简单WebSocket服务器的创建

    环境:Centos6.4,PHP环境:PHP7 服务端代码 <?php //创建websocket服务器 $host = '0.0.0.0'; $port = ; $ws = new swool ...

  5. Swoole学习(三)Swoole之UDP服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建UCP服务器(UDP服务器相对于TCP服务器通信更可靠些) /** * $host 是swoole需要监听的ip,如果要监听本 ...

  6. Swoole学习(二)Swoole之TCP服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...

  7. Swoole学习(一)了解一下Swoole的强大并在Centos安装Swoole及PHP扩展开启

    Swoole是面向生产环境的 PHP 异步网络通信引擎,官网:https://www.swoole.com/ 使 PHP 开发人员可以编写高性能的异步并发 TCP.UDP.Unix Socket.HT ...

  8. 01.swoole学习笔记--TCP服务器

    1.安装swoole扩展 2.网络调试助手进行调试 <?php //创建服务器 $host='192.168.10.31'; $port=; //$model='SWOOLE_PROCESS'; ...

  9. swoole学习--登录模块

    使用swoole+thinkphp6.0+redis 结合开发的登录模块,做完之后有几点感悟: 1.不要相信任务数据,包括请求的外部接口,特别是超时者部分,尽可能的交给task完成. 2.原来可以在入 ...

随机推荐

  1. LR11开始录制时打不开浏览器

    LR11 能支持的浏览器实际上不仅限于 IE8 官方文档没有明确表示支持,可以尝试.官方支持列表如下: ➤ Microsoft Internet Explorer 6.0 SP1 or SP2 ➤ M ...

  2. flexcan controller register

    /********************************************************************* * flexcan controller register ...

  3. Java HotSpot JVM垃圾回收

    一.J2SE 5.0 HotSpot JVM堆内存包括:年轻代.老年代.永久代 年轻代包括:Eden区.Survivor区 Survivor区包括:From区.To区 1. 年轻代GC(young g ...

  4. 从VS2010跳跃到VS2017

    Visual Studio 配色方案 https://studiostyl.es/ C#语言新特性 C#4.0:http://www.cnblogs.com/yangqi/archive/2010/0 ...

  5. ubuntu16.04中使用搜狗输入法Qt5无法输入中文解决方式

    1.安装fcitx-frontend-qt5 sudo apt-get install fcitx-frontend-qt5 2.将libfcitxplatforminputcontextplugin ...

  6. iOS常识名词解释 2016/04/05

    Bundle : http://www.cnblogs.com/BigPolarBear/archive/2012/03/28/2421802.html http://blog.sina.com.cn ...

  7. 每天一个linux命令(目录文件操作):【转载】linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令:  ls -lih 输出: [root@loc ...

  8. 每天一个linux命令:【转载】pwd命令

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...

  9. 接口测试框架——第六篇-读Excel封装方法

    谢谢@小麦苹果的提醒,才发现我借口测试少写了一个文件,今天给大家补上: common->service->excel_case_data.py # coding: utf-8 import ...

  10. c++ 中 毫秒级时间获取

    #include <time.h> clock_t start,ends; start=clock(); Sleep(); ends=clock(); cout<<ends-s ...