一、Swoole是啥子?
Swoole是一个PHP扩展。
支持异步特性、并行、高性能的网络通信引擎。
基于C语言编写的拓展。
异步多线程、异步TCP/Udp服务器,并且支持协程、消息队列和毫秒定时器,异步task/redis/文件任务,同样可以作为HTTP/WEBSOCKEt服务器。
创始人是大佬韩天峰。
地址:https://www.swoole.com/
二、swoole的特性
事件驱动的异步编程模式
异步TCP/UDP/HTTP/WebSocket/HTTP2协议的服务器端/客户端
支持IPv4/IPv6/UnixSocket/TCP/UDP
支持SSL/TLS隧道加密
支持并发百万TCP长连接
支持毫秒定时器
支持异步/同步/协程
支持CPU亲和性设置/守护进程
PHP 语言的异步多线程服务器
Swoole 使用纯 C 语言编写
三、服务器安装
版本环境:PHP7.2.2 + Swoole4.4
方法一:
git clone https://gitee.com/swoole/swoole.git
cd swoole
/usr/local/php7/bin/phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make && make install
vim /etc/php.ini
service php-fpm7 restart
php7 -m |grep swoole
问题:./swoole_config.h:22:2: error: #error “GCC 4.8 or later required.”
参考网址:https://blog.csdn.net/lanwilliam/article/details/77893033
解决:
curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo
yum install gcc gcc-g++ -y
gcc -v
方法二:
pecl install swolle
PS:强烈不推荐方法二、傻瓜式安装,但是也比较容易出现问题。
四、服务的运行流程图
简单梳理一下:
1.构建Server对象,就是实例化要创建的对象是内置的tcp还是udp
2.通过Set方法设置内置运行时参数
3.注册事件回调函数,也就是绑定连接、释放、方法
4.就是启动服务器,进程组启动监听
五、创建TCP服务器
<?php
//创建Server对象,监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);
$serv->set([
'work_num' => 8, //work进程数 cpu 1-4
'max_request' => 1000, //最大请求数量
]);
//监听连接进入事件
//$fd 客户端连接的唯一标识
//$reactor_id 线程id
$serv->on('connect', function ($serv, $fd , $reactor_id) {
echo "线程:{$reactor_id},标识:{$fd}";
echo "Client: Connect.\n";
});
//监听数据接收事件
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
$serv->send($fd, "Server: {$reactor_id}-{$fd}".$data);
});
//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
//启动服务器
$serv->start();
测试:telnet 127.0.0.1 9501
六、创建UDP服务器
<?php
//创建Server对象,监听 127.0.0.1:9502端口,类型为SWOOLE_SOCK_UDP
$serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
//监听数据接收事件
$serv->on('Packet', function ($serv, $data, $clientInfo) {
$serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
var_dump($clientInfo);
});
//启动服务器
$serv->start();
测试:
yum install nc -y
nc -u 127.0.0.1 9502
七、创建HTTP服务器
<?php
$http = new swoole_http_server('127.0.0.1',8811);
$http->set([
'enable_static_handler = true',
'document_root' => '/home/www'
]);//设定静态文件地址
$http->on('request', function ($request, $response) {
var_dump($request->get, $request->post);
$response->header("Content-Type", "text/html; charset=utf-8");
$response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();
测试:curl 127.0.0.1:8811 或者浏览器访问127.0.0.1:8811
八、websocket服务
1.什么是websocket服务
WebSocket协议是基于TCP的一种新的网络协议。
它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。
2.websocket特点
建立在Tcp协议之上的,性能开销小,通信高效,
客户端可以和任何服务器通信
协议标识符为ws wss
持久化网络通信协议
3.代码实现websocket实现
服务器端:
$server = new swoole_websocket_server("0.0.0.0", 9501);
$server->on('open', function (swoole_websocket_server $server, $request) {
echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('message', function (swoole_websocket_server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
客户端:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>ws测试<h1>
<script>
var wsUrl = "ws://39.106.53.36:9501";
var websocket = new WebSocket(wsUrl);
//实例对象的onopen属性
websocket.onopen = function(evt) {
websocket.send("hello-sinwa");
console.log("conected-swoole-success");
}
// 实例化 onmessage
websocket.onmessage = function(evt) {
console.log("ws-server-return-data:" + evt.data);
}
//onclose
websocket.onclose = function(evt) {
console.log("close");
}
//onerror
websocket.onerror = function(evt, e) {
console.log("error:" + evt.data);
}
</script>
</body>
</html>
测试的时候,可以直接F12的console里面直接看。
简单封装一下:
class Websocket {
CONST HOST = "0.0.0.0";
CONST PORT = 8812;
public $ws = null;
public function __construct() {
$this->ws = new swoole_websocket_server("0.0.0.0", 8812);
$this->ws->set(
[
'worker_num' => 2,
'task_worker_num' => 2,
]
);
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("task", [$this, 'onTask']);
$this->ws->on("finish", [$this, 'onFinish']);
$this->ws->on("close", [$this, 'onClose']);
$this->ws->start();
}
/**
* 监听ws连接事件
* @param $ws
* @param $request
*/
public function onOpen($ws, $request) {
echo "Connection:{$request->fd} has Ok\n";
}
/**
* 监听ws消息事件
* @param $ws
* @param $frame
*/
public function onMessage($ws, $frame) {
echo "ser-push-message:{$frame->data}\n";
// todo 10s
$data = [
'task' => 1,
'fd' => $frame->fd,
];
$ws->task($data);
$ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
}
/**
* @param $serv
* @param $taskId
* @param $workerId
* @param $data
*/
public function onTask($serv, $taskId, $workerId, $data) {
print_r($data);
// 耗时场景 10s
sleep(10);
return "on task finish"; // 告诉worker
}
/**
* @param $serv
* @param $taskId
* @param $data
*/
public function onFinish($serv, $taskId, $data) {
echo "taskId:{$taskId}\n";
echo "finish-data-sucess:{$data}\n";
}
/**/
public function onClose($ws, $fd) {
echo "clientid:{$fd}\n";
}
}
$obj = new Websocket();