<?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. mongodb 使用

    一.下载 MongoDB的官网是:http://www.mongodb.org/ MongoDB最新版本下载在官网的DownLoad菜单下:http://www.mongodb.org/downloa ...

  2. hexo部署Github博客

    例子:https://aquarius1993.github.io/blog/ 仓库:https://github.com/Aquarius1993/blog (前提是已经安装Xcode和git) 1 ...

  3. js之敏感词过滤

    HTML <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  4. 【剑指offer】 和为s的连续正数序列,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 设置两个辅助变量small和big,small表示序列的最小值,big表示序列的最大值.如果sum(small ~ big) > s,则增大sma ...

  5. softmax与logistic关系

    Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签  可以取两个以上的值. Softmax回归模型对于诸如MNIST手写数字分类等问题是很有用的,该问 ...

  6. 一次Mysql连接池卡死导致服务无响应问题分析(.Net Mysql.Data 6.9.9)

    问题: 进程启动后,线程数迅速上升至最小线程数后,缓慢上升(线程池限制)到数千,然后由于线程过多,CPU飙升到90%. 对外表现为Api无响应或连接超时. 背景 有些数据存在于另一个机房,通过内网专线 ...

  7. 拦截器springmvc防止表单重复提交【3】3秒后自动跳回首页【重点明白如何跳转到各自需要的页面没有实现 但是有思路】

    [1]定义异常类 [重点]:异常类有个多参数的构造函数public CmsException(String s, String... args),可以用来接受多个参数:如(“异常信息”,“几秒跳转”, ...

  8. linux自学(一)之vmware虚拟机安装

    之前有研究过linux,后来一段时间没有操作了,现在有点陌生,而且当初也没有记录学习内容.现在想从新开始包括虚拟机安装到部署Javaweb项目,把这之间所需要的全都记录下来,以便后边学习参考使用. 虚 ...

  9. BZOJ1412 ZJOI2009 狼和羊的故事 【网络流-最小割】

    BZOJ1412 ZJOI2009 狼和羊的故事 Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和 ...

  10. ORACLE PL/SQL:触发器

    ORACLE PL/SQL 触发器 本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8 ...