PHP implementation of simple HTTP server based on droole

It Abu 2020-11-07 22:16:41
php implementation simple http server

quote Swoole The official definition of :

PHP The asynchrony of language 、 parallel 、 High performance network communication framework , Use pure C Language writing , Provides PHP Asynchronous multithreaded server of language , asynchronous TCP/UDP Network client , asynchronous MySQL, Database connection pool ,AsyncTask, Message queue , Millisecond timer , Asynchronous file read and write , asynchronous DNS Inquire about .Swoole Can be widely used in the Internet 、 mobile communication 、 Enterprise software 、 Cloud computing 、 Network game 、 The Internet of things 、 Car networking 、 Smart home and other fields . Use PHP+Swoole As a network communication framework , Can make the enterprise IT The efficiency of the R & D team has been greatly improved , More focus on developing innovative products .

Briefly here swoole Two kinds of API Write simple HTTP The server .


Use swoole_server API, structure HTTP The server ,4 A step :

  1. structure Server object
  2. Set run-time parameters
  3. Register event callback function
  4. Start the server

Direct code embodies , newly build server.php

//1. structure Server object
$serv = new swoole_server("", 9501);
//2. Set run-time parameters
'worker_num' => 8,
'daemonize' => 0,
'max_request' => 10000,
'dispatch_mode' => 2,
'debug_mode'=> 1,
//3. Register event callback function
$serv->on('Receive', function($serv, $fd, $from_id, $data){
$respData='<h1>Hello Swoole.</h1>';
response($serv,$fd,$respData);// Encapsulate and send HTTP response message
//4. Start the server

How to package HTTP response message ? First you have to know HTTP The structure of the response message , Here's the picture

 Insert picture description here

Know the structure of the response message , Then my response message should be like this :

HTTP/1.1 200
<h1>Hello Swoole.</h1>

Code implementation .

* send content
* @param \swoole_server $serv
* @param int $fd
* @param string $respData
* @return void
function response($serv,$fd,$respData){
// Response line
$response = array(
'HTTP/1.1 200',
// Response head
$headers = array(
foreach($headers as $key=>$val){
$response[] = $key.':'.$val;
// Blank line
$response[] = '';
// Response body
$response[] = $respData;
$send_data = join("\r\n",$response);
$serv->send($fd, $send_data);

Here we are , A simple response Hello Swoole. Of HTTP The server is done . Complete code : here
( I can't visit gist? Configure local hosts file : )

function php server.php, Browser access, Of course, we often use Nginx As a front-end agent , Set up a test domain name, Local settings hosts mapping , You can access it through your domain name .Nginx To configure : here
Be careful : When the response message format is not correct , The browser page will always turn chrysanthemum , Wait for the request to return ...

later , With swoole_http_server,HTTP The server code becomes simpler !


$http = new swoole_http_server("", 9501);
$http->on('request', function ($request, $response) {
$html = "<h1>Hello Swoole.</h1>";

you 're right , In just a few lines of code . No need to encapsulate the response message .swoole_http_server Inherited from swoole_server, yes swoole built-in Http Server support , In a few lines of code, you can write an asynchronous non blocking multiprocessor Http The server .

Notice:swoole_http_server Yes Http Support for the agreement is not complete , Recommended as an application server only . And add... At the front end Nginx Acting as agent

Focus , Neverlost

All right, everyone , The above is the whole content of this article , You can see the people here , All are personnel . I said before ,PHP There are many technical points in this aspect , It's also because there are so many , I can't write it down , We won't read too much after writing it , So I've organized it into PDF And documentation , If you need anything, you can

Click to enter the code : PHP+「 platform 」

 Insert picture description here

 Insert picture description here

More learning can be found in 【 Benchmarking big factories 】 The high-quality goods PHP Architect tutorial catalog , As long as you can finish it, make sure your salary goes up one step ( Continuous updating )

I hope the above will help you , quite a lot PHPer There are always some problems and bottlenecks in the advanced stage , There is no sense of direction in the business code , I don't know where to start to improve , I've compiled some information about it , Including but not limited to : Distributed architecture 、 Highly scalable 、 High performance 、 High concurrency 、 Server performance tuning 、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql Optimize 、shell Script 、Docker、 Microservices 、Nginx If you need advanced advanced dry goods, you can share them for free , You can join me if you need PHP Technology exchange group

本文为[It Abu]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云