Home >Backend Development >PHP Tutorial >First introduction to Swoole in PHP, first introduction to PHPSwoole_PHP tutorial
Swoole is an advanced web development framework for PHP. The framework is not to improve the performance of the website, but to improve the development efficiency of the website. . Minimum performance loss in exchange for maximum development efficiency. Using the Swoole framework, developing a complex web function can be completed in a very short time.
Official definition:
Swoole: Redefining PHP
PHP’s asynchronous, parallel, high-performance network communication engine, written in pure C language, provides asynchronous multi-threaded server in PHP language, asynchronous TCP/UDP network client, asynchronous MySQL, asynchronous Redis, database connection pool, AsyncTask , message queue, millisecond timer, asynchronous file reading and writing, asynchronous DNS query. Swoole has built-in Http/WebSocket server/client and Http2.0 server.
Swoole can be widely used in the Internet, mobile communications, enterprise software, cloud computing, online games, Internet of Things, Internet of Vehicles, smart homes and other fields. Using PHP Swoole as a network communication framework can greatly improve the efficiency of enterprise IT R&D teams and focus more on developing innovative products.
Swoole extension installation and case source: http://wiki.swoole.com/wiki/page/6.html
Simple case:
<?php class Server { private $serv; public function __construct() { $this->serv = new swoole_server("0.0.0.0", 9501); $this->serv->set(array( 'worker_num' => 8, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 2, 'debug_mode' => 1 )); $this->serv->on('Start', array($this, 'onStart')); $this->serv->on('Connect', array($this, 'onConnect')); $this->serv->on('Receive', array($this, 'onReceive')); $this->serv->on('Close', array($this, 'onClose')); $this->serv->start(); } public function onStart($serv) { echo "Start\n"; } public function onConnect($serv, $fd, $from_id) { $serv->send($fd, "Hello {$fd}!"); } public function onReceive(swoole_server $serv, $fd, $from_id, $data) { echo "Get Message From Client {$fd}:{$data}\n"; } public function onClose($serv, $fd, $from_id) { echo "Client {$fd} close connection\n"; } } // 启动服务器 $server = new Server(); <?php class Client { private $client; public function __construct() { $this->client = new swoole_client(SWOOLE_SOCK_TCP); } public function connect() { if (!$this->client->connect("127.0.0.1", 9501, 1)) { echo "Error: {$fp->errMsg}[{$fp->errCode}]\n"; } $message = $this->client->recv(); echo "Get Message From Server:{$message}\n"; fwrite(STDOUT, "请输入消息:"); $msg = trim(fgets(STDIN)); $this->client->send($msg); } } $client = new Client(); $client->connect();
Open two terminals respectively and enter: php server.php | php client.php to see the effect!
Swoole function introduction
includes the following special functions:
1. ORM-like data query provides a SQL wrapper to seamlessly combine MySQL's SQL with PHP's Array, session, and Cache.
2. App MVC layered structure, effective program structure layering, improves program maintainability and scalability, achieves low coupling, and is developed based on interfaces.
3. Integrate a large number of practical functions, such as convenient database operations, template operations, cache operations, system configuration, form processing, paging, data calling, dictionary operations, upload processing, content editing, debugging, etc.
4. Template-data reflection system, which can directly call data in the template and provide many tags. However, there is no need to modify the program. Just modify the template to realize various updates and maintenance of the website.
A few other functions
1. Swoole contains a large number of classes and provides numerous functional extensions. Basically, most of the functional classes that can be used in web development can be found in the Swoole framework.
2. Swoole has a plug-in system, Fckeditor, Adodb, pscws Chinese word segmentation, Chinese full-text indexing system, the latest Key-Value database idea, TokyoTyrant, which can infinitely expand the functions of the framework.