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 .
swoole_server
Use swoole_server API, structure HTTP The server ,4 A step :
- structure Server object
- Set run-time parameters
- Register event callback function
- Start the server
Direct code embodies , newly build server.php:
<?php
//1. structure Server object
$serv = new swoole_server("0.0.0.0", 9501);
//2. Set run-time parameters
$serv->set(array(
'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
$serv->start();
How to package HTTP response message ? First you have to know HTTP The structure of the response message , Here's the picture
Know the structure of the response message , Then my response message should be like this :
HTTP/1.1 200
Server:SwooleServer
Content-Type:text/html;charset=utf8
Content-Length:13
<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(
'Server'=>'SwooleServer',
'Content-Type'=>'text/html;charset=utf8',
'Content-Length'=>strlen($respData),
);
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 : 192.30.252.141 gist.github.com )
function php server.php
, Browser access http://127.0.0.1:9501/, Of course, we often use Nginx As a front-end agent , Set up a test domain name www.server.com, 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 !
swoole_http_server
<?php
$http = new swoole_http_server("127.0.0.1", 9501);
$http->on('request', function ($request, $response) {
$html = "<h1>Hello Swoole.</h1>";
$response->end($html);
});
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 」
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