swoole httpserver学习
文件 HttpServer.php
<?php
/**
* Created by PhpStorm.
* User: mac
* Date: 2019/9/13
* Time: 21:00
*/ class HttpServer
{
public $http_serv; const PORT = 9501;
const IP = "0.0.0.0"; public $static_handel_switch = true;
public $document_root = '/www/swoole'; public function __construct()
{
$this->http_serv = new Swoole\Http\Server(self::IP,self::PORT); $this->http_serv->on("request",[$this,"onRequest"]); if($this->static_handel_switch == true){
$this->enableStaticHandel($this->document_root);
}
} /**
* 接受到客户端请求
* @param $request 请求
* @param $response 响应
*/
public function onRequest($request,$response)
{ //var_dump($request->server['request_uri']);
if($request->server['request_uri'] != "/favicon.ico"){ $headers = [
'Content-Type'=>"text/html; charset=utf-8"
]; $this->setHeader($headers,$response); $response->end("<h1>hello swoole ".rand(100,999)."</h1>");
} } public function setHeader(array $headers ,$response)
{
foreach($headers as $key=>$header){
$response->header($key,$header);
}
} /**
* 启动
*/
public function start()
{
$this->http_serv->start();
} public function enableStaticHandel($document_root)
{
$this->http_serv->set(
[
'document_root' => $document_root, // v4.4.0以下版本, 此处必须为绝对路径
'enable_static_handler' => true,
]
);
}
} $http = new HttpServer();
$http->start();
cli执行
php HttpServer.php
浏览器访问
http://192.168.1.200:9501 ip为自己虚拟机ip
swoole httpserver学习的更多相关文章
- Swoole 入门学习(二)
Swoole 入门学习 swoole 之 定时器 循环触发:swoole_timer_tick (和js的setintval类似) 参数1:int $after_time_ms 指定时间[毫秒] ...
- swoole深入学习 8. 协程 转
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yangyi2083334/article/ ...
- Swoole 简单学习(2)
Swoole 简单学习(2) swoole之tcp服务器: //创建tcp服务器new swoole_server(string $host,int $port,int $mode=SWOOLE_PR ...
- swoole深入学习 2. tcp Server和tcp Client
这节来学习Swoole最基础的Server和Client.会通过创建一个tcp Server来讲解. server <?php class Server { private $serv; pub ...
- swoole深入学习 1. swoole初始
0. 前言 swoole在PHP圈火了这么久,从2年前我用node写socket聊天服务器的时候就火了,那时候,经常有类似的文章php+swoole完爆nodejs之类的文章来吸引眼球,先不说它的好与 ...
- Swoole源代码学习记录(十五)——Timer模块分析
swoole版本号:1.7.7-stable Github地址:点此查看 1.Timer 1.1.swTimer_interval_node 声明: // swoole.h 1045-1050h ty ...
- swoole深入学习 4. process
swoole-1.7.2增加了一个进程管理模块,用来替代PHP的pcntl扩展.pcntl是php新增的一个多进程扩展,用来实现多进程,但是有很多不完善的地方,swoole 就完善了这些地方,而且使得 ...
- swoole深入学习 3. upd Server和udp Client
前面主要讲了tcp得server和client的业务处理,tcp有三次握手,有连接的概览,而UDP服务器与TCP服务器不同,UDP没有连接的概念.启动Server后,客户端无需Connect,直接可以 ...
- Swoole源代码学习记录(十三)——Server模块具体解释(上)
Swoole版本号:1.7.5-stable Github地址:https://github.com/LinkedDestiny/swoole-src-analysis 最终能够正式进入Server. ...
随机推荐
- HTTP和WSGI协议
HTTP协议简介 超文本传输协议(HyperText Transfer Protocol)是一种应用层协议.HTTP是万维网的数据通信的基础.设计HTTP最初的目的是为了提供一种发布和接收HTML页面 ...
- Qps 和 tps的解释
QPS:Queries Per Second意思是“每秒查询率”,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS:是Transactions ...
- CerntOS7下搭建git服务器
(1).安装git yum安装git,需要ssh的支持.某些版本需要安装git-core,那才是服务器. [root@youxi1 ~]# yum -y install git 创建git用户 [ro ...
- CentOS 端口和防火墙操作
Centos 7 端口和防火墙命令: 查看已经开放的端口:firewall-cmd --list-ports 开启端口:firewall-cmd --zone=public --add-port=80 ...
- spark中的cache和persist的区别
在使用中一直知其然不知其所以然的地使用RDD.cache(),系统的学习之后发现还有一个与cache功能类似看起来冗余的persist 点进去一探究竟之后发现cache()是persist()的特例, ...
- 直接用的tar压缩
将目录里的doc文件夹压缩成tar.gz tar -czf doc.tar.gz doc 解压tar.gz tar -xzvf doc.tar.gz 在不解压的情况下查看压缩包的内容: tar ...
- iOS 在iphoneX上运行的app没有icon图标,在其他手机上有图标
今天朋友问了一个问题,在iPhoneX上运行的app没有icon图标,在其他手机上有图标. 一开始我以为是没放iPhoneX的图标,后面朋友解决了,告诉了我原因,这里记录下吧: 原因: 图标格式问题, ...
- 【Leetcode_easy】784. Letter Case Permutation
problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- dozer工具类
jar:commons-beanutils-1.9.3.jar.commons-lang-2.6.jar.dozer-5.3.2.jar.jcl-over-slf4j-1.7.25.jar.slf4j ...