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. ...
随机推荐
- Linux hostname介绍
以下操作均时基于 Centos 6.8 操作. 一.现象 在平时工作中,当需要修改主机名时,我们一般会这样操作: 第一步,通过 hostname 命令临时修改主机名. hostname kwang-t ...
- 开始学习Docker啦--容器理论知识(一)
目录 一.容器核心技术 1.容器规范 2.容器 runtime 3.容器管理工具 4.容器定义工具 5.Registry 6.容器 OS 二.说说容器 1.什么是容器 Containers vs. v ...
- 【C#设计模式1】单例模式
一.引言 最近在设计模式的一些内容,主要的参考书籍是<Head First 设计模式>,同时在学习过程中也查看了很多博客园中关于设计模式的一些文章的,在这里记录下我的一些学习笔记,一是为了 ...
- linux centos7 开启 mysql 3306 端口 外网访问 的实践
第〇步:思路 3306 端口能否被外网访问,主要要考虑: (1)mysql的3306 端口是否开启?是否没有更改端口号? (2)mysql 是否允许3306 被外网访问? (3)linux 是否已经开 ...
- Jenkins持续集成企业实战
阅读目录: Jenkins持续集成企业实战1.1 目前主流网站部署的流程1.2 Jenkins持续集成简介1.3 Jenkins持续集成组件1.4 Jenkins平台安装部署1.5 Jenkins相关 ...
- IntelliJ IDEA 简单设置
1.在idea中添加try/catch的快捷键 ctrl+alt+t 2.主题修改 选择菜单栏“File--settings--apperance--theme”,主题选择Darcula: 3.代码字 ...
- python 字符串 - python基础入门(12)
在 python变量 文章中我们对python变量做了一个简单的了解,整数/浮点数/bool值相对来讲都比较简单,今天详细在讲解一下关于字符串的内容,字符串俗称:str. 在本文会大量的使用print ...
- CNN-3: VGGNet 卷积神经网络模型
1.VGGNet 模型简介 VGG Net由牛津大学的视觉几何组(Visual Geometry Group)和 Google DeepMind公司的研究员一起研发的的深度卷积神经网络,在 ILSVR ...
- [转帖]18W喂不饱有必要买30W充电器吗?iPhone 11 Pro Max充电评测
18W喂不饱有必要买30W充电器吗?iPhone 11 Pro Max充电评测 https://www.cnbeta.com/articles/tech/895237.htm 改天买一个 设备玩一玩 ...
- SSH简介及两种远程登录的方法
出处 https://blog.csdn.net/li528405176/article/details/82810342 目录 SSH的安全机制 SSH的安装 启动服务器的SSH服务 SSH两种级别 ...