swoole学习
<?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模式下
Reactor和Worker是同一个角色
swoole_websocket_server 继承自 swoole_http_server,swoole_http_server 继承自 swoole_server,所有swoole_server的方法,都可以用,Onmessage方法是swoole_websocket_server是用到的。
swoole学习的更多相关文章
- swoole学习(二)----搭建server和client
1.搭建server 1.1搭建server.php 1.搭建websocket服务器,首先建立 server.php 文件, <?php $server = new swoole_websoc ...
- swoole学习(一)----linux安装swoole
1.下载swoole 登录swoole官网 https://www.swoole.com/ 点击下载,找到github或者其他链接下载下来 放到虚拟机上. 也可以使用虚拟机下载 2.登录虚拟机 推荐使 ...
- Swoole学习(七)Swoole之异步TCP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...
- Swoole学习(五)Swoole之简单WebSocket服务器的创建
环境:Centos6.4,PHP环境:PHP7 服务端代码 <?php //创建websocket服务器 $host = '0.0.0.0'; $port = ; $ws = new swool ...
- Swoole学习(三)Swoole之UDP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建UCP服务器(UDP服务器相对于TCP服务器通信更可靠些) /** * $host 是swoole需要监听的ip,如果要监听本 ...
- Swoole学习(二)Swoole之TCP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...
- Swoole学习(一)了解一下Swoole的强大并在Centos安装Swoole及PHP扩展开启
Swoole是面向生产环境的 PHP 异步网络通信引擎,官网:https://www.swoole.com/ 使 PHP 开发人员可以编写高性能的异步并发 TCP.UDP.Unix Socket.HT ...
- 01.swoole学习笔记--TCP服务器
1.安装swoole扩展 2.网络调试助手进行调试 <?php //创建服务器 $host='192.168.10.31'; $port=; //$model='SWOOLE_PROCESS'; ...
- swoole学习--登录模块
使用swoole+thinkphp6.0+redis 结合开发的登录模块,做完之后有几点感悟: 1.不要相信任务数据,包括请求的外部接口,特别是超时者部分,尽可能的交给task完成. 2.原来可以在入 ...
随机推荐
- tomcat的localhost-config is missing 错误的解决方法
运行项目时报错,错误信息为: The tomcat server configuration at /sever/tomcat v7.0 localhost-config is missing 解决方 ...
- emacs编辑器的使用
尽管有许多ide,但是,各种软件使用各种不同,设置等,受不了.觉得应该有一手好的编辑器就可以了.尤其是受不了ide什么lib,.h,exe,dll等路径设置,让人心烦.再加上黑盒子的感觉很不爽.一旦出 ...
- linux前奏
1:选典型 2:选稍后自定义安装系统 3: 4: :5:弹出清单 二:装系统 下载linux的网址:https://mirrors.aliyun.com/centos/ 1: 2: 3: 2:如何修改 ...
- [转]redis服务器与客户端保活参数(tcp-keepalive)设置
最近使用redis的list做跨进程的消息队列,客户端使用的是redis-cplusplus-client.这个client库还是蛮好用的,提供了和redis命令行一致的接口,很方便. 使用过程中发现 ...
- MPI 学习
一.编译MPI mpic++ test.cc -o test 二.启动MPI mpiexec -np 10 ./test 三.几个例子 第一个进程向第二个发一个数,第二个进程向第三个进程发送一个数.. ...
- manacher 算法 这个人确实写得太好了;
O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串. ...
- 【备份】 解决acer v5 471g arch关机后自动重启的问题
Fedora 17 on an Aspire V5-571 -- Reboot on Shutdown13 FEBRUARY 2015Update on 2/13/15: This article w ...
- BZOJ4975: [Lydsy1708月赛]区间翻转( 博弈&逆序对)
4975: [Lydsy1708月赛]区间翻转 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 265 Solved: 140[Submit][Stat ...
- BZOJ4361 isn 【树状数组优化DP】*
BZOJ4361 isn Description 给出一个长度为n的序列A(A1,A2-AN).如果序列A不是非降的,你必须从中删去一个数,这一操作,直到A非降为止.求有多少种不同的操作方案,答案模1 ...
- 剑指offer第四章
剑指offer第四章 1.二叉树的镜像 二叉树的镜像:输入一个二叉树,输出它的镜像 分析:求树的镜像过程其实就是在遍历树的同时,交换非叶结点的左右子结点. 求镜像的过程:先前序遍历这棵树的每个结点,如 ...
//ini_set('default_socket_timeout', -1);

具体参照swoole官网文档:SWOOLE_BASE模式下