文件 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学习的更多相关文章

  1. Swoole 入门学习(二)

    Swoole 入门学习 swoole 之 定时器 循环触发:swoole_timer_tick    (和js的setintval类似) 参数1:int $after_time_ms 指定时间[毫秒] ...

  2. swoole深入学习 8. 协程 转

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yangyi2083334/article/ ...

  3. Swoole 简单学习(2)

    Swoole 简单学习(2) swoole之tcp服务器: //创建tcp服务器new swoole_server(string $host,int $port,int $mode=SWOOLE_PR ...

  4. swoole深入学习 2. tcp Server和tcp Client

    这节来学习Swoole最基础的Server和Client.会通过创建一个tcp Server来讲解. server <?php class Server { private $serv; pub ...

  5. swoole深入学习 1. swoole初始

    0. 前言 swoole在PHP圈火了这么久,从2年前我用node写socket聊天服务器的时候就火了,那时候,经常有类似的文章php+swoole完爆nodejs之类的文章来吸引眼球,先不说它的好与 ...

  6. Swoole源代码学习记录(十五)——Timer模块分析

    swoole版本号:1.7.7-stable Github地址:点此查看 1.Timer 1.1.swTimer_interval_node 声明: // swoole.h 1045-1050h ty ...

  7. swoole深入学习 4. process

    swoole-1.7.2增加了一个进程管理模块,用来替代PHP的pcntl扩展.pcntl是php新增的一个多进程扩展,用来实现多进程,但是有很多不完善的地方,swoole 就完善了这些地方,而且使得 ...

  8. swoole深入学习 3. upd Server和udp Client

    前面主要讲了tcp得server和client的业务处理,tcp有三次握手,有连接的概览,而UDP服务器与TCP服务器不同,UDP没有连接的概念.启动Server后,客户端无需Connect,直接可以 ...

  9. Swoole源代码学习记录(十三)——Server模块具体解释(上)

    Swoole版本号:1.7.5-stable Github地址:https://github.com/LinkedDestiny/swoole-src-analysis 最终能够正式进入Server. ...

随机推荐

  1. Qt编写自定义控件54-时钟仪表盘

    一.前言 这个控件没有太多的应用场景,主要就是练手,论美观的话比不上之前发过的一个图片时钟控件,所以此控件也是作为一个基础的绘制demo出现在Qt源码中,我们可以在Qt的安装目录下找到一个时钟控件的绘 ...

  2. Node.js报错TypeError: Cannot read property 'isDirectory' of undefined

    截图如下: 原因如下:记住"./uploads" 后要加一个/ fs.stat("./uploads/" + files[i], function(err, s ...

  3. nfs搭建;nfs监控;mount对于nfs的相应配置

    nfs搭建 https://www.cnblogs.com/lms0755/p/9211585.html https://www.jianshu.com/p/e47cccbb3ae5 https:// ...

  4. 游戏开发中伪随机正态分布JavaScript

    在游戏开发中经常遇到随机奖励的情况,一般会采取先生成数组,再一个一个取的方式发随机奖励. 下面是js测试正态分布代码: <!DOCTYPE html> <html lang=&quo ...

  5. 2019-11-8 Linux作业 李宗盛

    linux系统的特点:完全免费,高效,安全稳定.支持多种硬件平台.有好的用户界面.强大的网络功能.支持多任务多用户. linux一般有三个主要部分:内核.命令解释层.实用工具. 那合适系统的心脏,是运 ...

  6. python3.7.3安装beautifulsoup4出现版本不兼容的问题

    今天想安装一个beautifulsoup4,结果一直出错,好多教程总是有各种坑……找了很多个教程,为了记录方法也为了分享给大家,简单些一个.但是是真真不想再费劲写一遍了……直接贴链接,亲测有效,但是底 ...

  7. 关于PADS的一些概念和实用技巧(一)

    关于PADS的一些概念和实用技巧(一) 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 1. 关于part,CAE Decal,PCB Decal Part ...

  8. Codis的安装配置

    codis是分布式redis解决方案 centos系统下安装codis需要安装相关的依赖,将图中的依赖上传至centos系统中 依次进行安装: 一.go环境的安装配置 解压到install目录下: . ...

  9. windows服务器入门 初始化数据盘

    本人在寒假的时候自行搭建了一个服务器,在此分享一下我的方法.本人服务器的系统为Windows 2012R2 在后面的讲解中中文英文都会有    所以不用在意系统的语言问题 1)第一步  自然就是打开服 ...

  10. 49.Django起步学习

    django起步 django安装 pip install django==2.0.4(版本号) pip install django 默认安装最新版本 创建项目 django-admin start ...