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. ...
随机推荐
- toast提示信息获取和Monkey笔记
获取toast toast提示信息出现场景:用户输入用户名和密码后,提示的'登录成功', 用之前的定位方法获取不了,需要Uiautomator2来获取 安装node.js (使用 npm 或 node ...
- iOS技术面试02:内存管理
怎么保证多人开发进行内存泄露的检查. 如何定位内存泄露? 1> 使用Analyze进行代码的静态分析(检测有无潜在的内存泄露) 2> 通过leak检查在程序运行过程中有无内存泄露 3> ...
- 安装android sdk,后出现导出错误,提示命令行找不到解决方案
The steps. Rename android sdk tool folder : [Your Android SDK root]/tools -> toolsXXXX Download S ...
- 利用socket编程在ESP32上搭建一个TCP客户端
通过之前http://www.cnblogs.com/noticeable/p/7636582.html中对socket的编程,已经可以知道如何通过socket编程搭建服务器和客户端了,现在,就在ES ...
- VMware虚拟机中CentOS7的硬盘空间扩容
查看centos7系统挂载点信息 扩展VMWare-centos7硬盘空间 对新增加的硬盘进行分区.格式化 添加新LVM到已有的LVM组,实现扩容 1.查看centos7系统挂载点信息 df -h查看 ...
- CentOS系统安装配置JDK
我们可以通过xftp工具将jdk文件上传至CentOS系统指定文件夹中 一.安装jdk 进入jdk存放目录,将jdk解压至install文件夹中: tar -zxf jdk-8u151-linux-x ...
- 路由器04--OPKG
1.简介 https://oldwiki.archive.openwrt.org/doc/techref/opkg Opkg 是一个基于 ipkg 的轻量级的软件包管理系统,主要用于嵌入式系统,目前应 ...
- 使用 netkeeper 创翼网速慢解决方案(13)
1. 方法1 步骤: 卸载Netkeeper,并删除 卸载以太网(本地连接)驱动 重置网络 重启 重新安装Netkeeper.如果登录出错,卸载「IP,IPv6,Network Monitor」,然后 ...
- 【C++札记】类的分离式写法
介绍 类的分离式写法,使得代码更加规范,增强了阅读性. 分离式写法的规则: 1.类的变量:写在类的里面 2.成员函数:类中写函数的声明,函数的定义写在类体外. 3.写在类外函数定义时,类名前加限定(O ...
- python学习--12 基本数据类型
数字 int -int 功能 1.转换 例如: a = '123' # 字符串print(type(a),a)b = int(a) # 将字符串转换成intprint(type(b),b) 运算结果 ...