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.原来可以在入 ...
随机推荐
- CSS样式让元素填充剩余部分为自己的高度或宽度
#nav { background-color: #85d989; width: 100%; height: 50px; } #content { background ...
- vscode+Firefox实现前端移动真机测试
需要配件: 1.安装有火狐浏览器的移动端(手机); 2.安装有火狐浏览器和vscode的pc(电脑); 3.在vscode安装Live Server 插件 4.安装之后vscode右下角会有Go Li ...
- iOS-----AVFoundation框架的功能详解
使用AVFoundation拍照和录制视频 需要开发自定义的拍照和录制视频功能,可借助于AVFoundation框架来实现,该框架提供了大量的类来完成拍照和录制视频.主要使用如下类: AVCaptur ...
- stark组件
写一个stark组件仿造admin的功能 1:新建一个stark的app 问题:在django每次启动会扫描目录下所有的admin文件,需要扫描项目目录下的每个stark文件,我们需要怎么做 1:看在 ...
- learn go return fuction
package main // 参考文章: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.9.md im ...
- phpcms v9取消验证码
phpcms/modules/admin/index.php// $code = isset($_POST['code']) && trim($_POST['code']) ? tri ...
- redhat7学习笔记之从零到部署javaweb项目
REDHAT7学习笔记 1. 安装vmware10 安装过程略,下载地址:链接: https://pan.baidu.com/s/16odKKkRYBxGWDVo1cz_wxA 注意,10以上版本不在 ...
- FastJson的一些使用
前言 最近经常使用json的一些转换,使用的是fastjson,所以就对fastjson进行了一些汇总,记录下来. 正文 主要的api 首先是一些类库的说明: SerializeWriter:相当于S ...
- Java 自定义FTP连接池
转自:https://blog.csdn.net/eakom/article/details/79038590 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
- 让thinkphp 5 支持pathinfo 的 nginx ,去掉index.php
在TP5.0中查阅tp5官方文档,注意:5.0取消了URL模式的概念,并且普通模式的URL访问不再支持.phthinfo 是什么? PHP中的全局变量$_SERVER['PATH_INFO']是一个很 ...
//ini_set('default_socket_timeout', -1);

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