- C语言编写PHP扩展(so文件)
- 异步,并行(多线程),高性能网络通信引擎
- 协程
- 应用: 互联网,网络游戏,聊天室,在线直播
gcc autoconfig tar -jxvf php-7.2.7.tar.bz2 ./configure & make & make install vim ~/.bash_profile source ~/.bash_profile php.ini文件存放位置 [root@bogon php]# php -i | grep php.ini Configuration File (php.ini) Path => /home/work/soft/php/libSwoole Install
git clone https://gitee.com/swoole/swoole.git phpize ./configure --with-php-config=/home/work/soft/php/bin/php-config make & make installTcp Server
查看进程个数
·ps aft | grep tcp.php·
set([
'worker_num' => 6 , // worker进程数 cpu 1-4
'max_request' => 10000,
]);
//监听连接进入事件
$serv->on('connect', function ($serv, $fd, $reactor_id) {
echo "Client: {$reactor_id} - {$fd}-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();
TCP Client
connect("127.0.0.1", 9501)) {
echo "连接失败";
exit;
}
/// php cli常量
fwrite(STDOUT, "请输入消息:");
$msg = trim(fgets(STDIN));
// 发送消息给 tcp server服务器
$client->send($msg);
// 接受来自server 的数据
$result = $client->recv();
echo $result;
Http service
// 0.0.0.0b表示监听所有地址
$http = new swoole_http_server("0.0.0.0", 8811);
$http->set(
[
// 如果是静态文件,不走下面的数据逻辑
'enable_static_handler' => true,
// 静态资源存放路径
'document_root' => "/home/work/hdtocs/swoole_mooc/data",
]
);
$http->on('request', function($request, $response) {
//print_r($request->get);
$content = [
'date:' => date("Ymd H:i:s"),
'get:' => $request->get,
'post:' => $request->post,
'header:' => $request->header,
];
swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename){
// todo
}, FILE_APPEND);
$response->cookie("hanxiao", "xsssss", time() + 1800);
$response->end("It's fucking end ". json_encode($request->get));
});
$http->start();
Swoole Websocket
hanxiao-swoole-ws测试
$server = new swoole_websocket_server("0.0.0.0", 8812);
//$server->set([]);
$server->set(
[
'enable_static_handler' => true,
'document_root' => "/home/work/hdtocs/swoole_mooc/data",
]
);
//监听websocket连接打开事件
$server->on('open', 'onOpen');
function onOpen($server, $request) {
print_r($request->fd);
}
// 监听ws消息事件
$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, "singwa-push-secesss");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closedn";
});
$server->start();
优化 Swoole Task
客户端不用等待Task完成
class Ws {
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();
}
public function onOpen($ws, $request) {
var_dump($request->fd);
if($request->fd == 1) {
// 每2秒执行
swoole_timer_tick(2000, function($timer_id){
echo "2s: timerId:{$timer_id}n";
});
}
}
public function onMessage($ws, $frame) {
echo "ser-push-message:{$frame->data}n";
// todo 10s
$data = [
'task' => 1,
'fd' => $frame->fd,
];
//$ws->task($data);
swoole_timer_after(5000, function() use($ws, $frame) {
echo "5s-aftern";
$ws->push($frame->fd, "server-time-after:");
});
$ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
}
public function onTask($serv, $taskId, $workerId, $data) {
print_r($data);
// 耗时场景 10s
sleep(10);
return "on task finish"; // 告诉worker
}
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 Ws();


