<?php
// 编写自己的http服务器
/**
* 第1个参数, 0.0.0.0 表示监听本机所有的网址地址
* 第2参数,6060 表示监听端口号 当前建议1-1024之间是系统保留不要去用
*/
$http = new swoole_http_server('0.0.0.0', 6060);
// 设置
$http->set([
// 工作的进程数量
'worker_num' => 2,
// 一个进程请求的最大次数
'max_request' => 1000
]);
// 相关事件的监听
// 开始事件
$http->on('start', function (swoole_server $server) {
echo "服务启动了!\n";
});
// 处理请求事件
/**
* $request 请求对象 服务器请求得到客户发过来的数据对象
* $response 响应对象 服务器向客户发送数据所用的对象
*/
$http->on('request', function (swoole_http_request $request, swoole_http_response $response) {
// 加上头信息
$response->header('Content-Type', 'text/html;charset=utf-8');
$response->end('你好世界');
});
// 启动服务
$http->start();