文件 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. ElasticSearch——路由(_routing)机制

    前言 一条数据是如何落地到对应的shard上的? 当索引一个文档的时候,文档会被存储到一个主分片中. Elasticsearch 如何知道一个文档应该存放到哪个分片中呢? 首先这肯定不会是随机的,否则 ...

  2. Java使用Apache Commons Exec运行本地命令行命令

    首先在pom.xml中添加Apache Commons Exec的Maven坐标: <!-- https://mvnrepository.com/artifact/org.apache.comm ...

  3. hana客户端工具

    SAP HANA可视化客户端工具是C/S模式的,远程访问使用,都不是太方便,目前有一款基于WEB的可视化工具TreeSoft,通过浏览器就可以访问使用了,并且可以同时管理.维护.监控SAP HANA等 ...

  4. 最新 草花互动java校招面经(含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.草花互动等10家互联网公司的校招Offer,因为某些自身原因最终选择了草花互动.6.7月主要是做系统复习.项目复盘.Leet ...

  5. css3 float排序问题

    css3 float排序问题 有时候发现 会错位 那是因为有个图片大小不一致才会这样 所以要确保每个图片一样

  6. Java使用icepdf转高清图片

    <dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-core</ar ...

  7. IO-file-06 文件夹的遍历

    /** * 列出下一级 * 1.list():  列出下级名称  String[] list = dir.list(); * 2.listFiles():列出下级File对象 File[] listF ...

  8. 【AtCoder】diverta 2019 Programming Contest 2

    diverta 2019 Programming Contest 2 A - Ball Distribution 特判一下一个人的,否则是\(N - (K - 1) - 1\) #include &l ...

  9. 分布式架构下,session共享有什么方案么?

    分布式架构下,session共享有什么方案么? 会点代码的大叔 科技领域创作者 分布式架构下的session共享,也可以称作分布式session一致性:关于这个问题,和大家说一说解决方案(如果有其他的 ...

  10. libevent实现TCP 服务端

    libevent实现Tcp Server基于bufferevent实现 /******************************************************** Copyri ...