ThinkPHP is an open source PHP framework designed to simplify enterprise application development and agile WEB application development. Using the object-oriented development structure and MVC pattern, combining Struts Action, Dao idea, JSP TagLib (tag library), RoR ORM mapping and ActiveRecord pattern, encapsulating CURD and some common operations, single entry mode, etc. It has unique performance in template engine, caching mechanism, authentication mechanism and extensibility.

Swoole is a PHP asynchronous, parallel, high-performance network communication engine, written in pure C language. Provides the PHP language asynchronous multithreaded server, asynchronous TCP/UDP network client, asynchronous MySQL, asynchronous Redis, database connection pool, AsyncTask, message queue, millisecond timer, asynchronous file read and write, asynchronous DNS query.

Swoole.php configuration file, then set:

<! -? phpreturn [ 'host' =--> 'tp5.com', 'port' => 9508,]; Swoole's own configuration parameters can be set, such as <! -? phpreturn [ 'host' =--> 'tp5.com', 'port' => 9508, 'worker_num' => 4, 'max_request' => 1000,];Copy the code

The onWorkerStart and onRequest event callback methods are defined in the extension (please do not replace them if you are not familiar with them). If you want to customize the Swoole event callback method, you can use the closure definition in the configuration file.

<! -? phpreturn [ 'host' =--> 'tp5.com', 'port' => 9508, 'worker_num' => 4, 'max_request' => 1000, $worker_id => function($server, $worker_id){// add your code},];Copy the code

Or add it directly to the configuration file

Use Swoole as the Server Server

– It is possible to launch a Swoole Server directly (requires version 2.0.9+)

php think swoole:server

Starts a Websocket service at 0.0.0.0:9508.

If you need to customize parameters, you can configure them in config/swoole_server.php

Include:



All swoole parameters are supported.

The use of closures to define related event callbacks is also supported.

Expand own configuration return / / / 'host' = > '0.0.0.0', / / listening address 'port' = > 9501, / / listener ports' type '= >' socket, HTTP server 'mode' => SWOOLE_PROCESS, 'socket_type' => SWOOLE_SOCK_TCP, $request => function ($server, $request) {echo "server: handshake success with fd{$request->fd}\n"; }, 'onMessage' => function ($server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $server->push($frame->fd, "this is server"); }, 'onRequest' => function ($request, $response) { $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>"); }, 'onClose' => function ($ser, $fd) { echo "client {$fd} closed\n"; },];Copy the code

You can also use custom service classes

<! -? php namespace app\http; use think\swoole\Server; Class Swoole extends Server{protected $host = '127.0.0.1'; protected $port = 9502; protected $option = [ 'worker_num'=--> 4, 'daemonize' => true, 'backlog' => 128 ]; public function onReceive($server, $fd, $from_id, $data) { $server->send($fd, 'Swoole: '.$data); }}Copy the code

All callback method definitions of Swoole are supported (the callback method must be of public type). If the serverType attribute is defined as socket or HTTP, swoole_webSocket_server and SWOole_HTTP_server of Swoole are supported Then add the configuration parameters in swoole_server.php:

return [
    'swoole_class'  =>   'app\http\Swoole',];
Copy the code

After this parameter is defined, other configuration parameters are invalid.

Then the command line starts the server

PHP think swoole: server support reload | restart | stop | operation status

php think swoole:server reload