The following column workerman tutorial will introduce the concept and usage of workerman to you. I hope it will be helpful to friends in need!
workerman is a high-performance PHP socket server framework. Workerman is based on PHP multi-process and libevent event polling library. PHP developers only need to implement one or two interfaces. Develop your own network applications, such as Rpc services, chat room servers, mobile game servers, etc.
Workerman's goal is to make it easier for PHP developers to develop high-performance socket-based application services without having to understand the details of PHP sockets and PHP multi-processes.
Workerman itself is a PHP multi-process server framework with modules for PHP process management and socket communication, so it can run independently without relying on containers such as php-fpm, nginx or apache.
PHP process management: similar to the process management of the operating system.
socket: Two programs on the network exchange data through a two-way communication connection. One end of this connection is called a socket.
Server-side development example WebSocket Server
1. Create a new file start.php
<?php use Workerman\Worker; require_once './Workerman/Autoloader.php'; // 创建一个Worker监听2346端口,使用websocket协议通讯 $ws_worker = new Worker("websocket://0.0.0.0:2346"); // 启动4个进程对外提供服务 $ws_worker->count = 4; // 当收到客户端发来的数据后返回hello $data给客户端 $ws_worker->onMessage = function($connection, $data) { // 向客户端发送hello $data $connection->send('hello ' . $data); }; // 运行 Worker::runAll();
2. Start the service:
php start.php start -d
Use the above statement to start start.php, create a Worker to listen to port 2346, use the websocket protocol to communicate and start four processes to provide external services. When a request is received, data will be sent to the client.
3. Check the running status of workererman:
php start.php status
Several major features of workerman:
Pure PHP development
Workerman is completely developed using PHP. Applications developed using Workerman can run independently without relying on containers such as php-fpm, apache, and nginx. This makes it very convenient for PHP developers to develop, deploy, and debug applications.
Support PHP multi-process
In order to give full play to the performance of the server's multi-CPU, Workerman supports multi-process and multi-tasking by default. Workerman starts a main process and multiple sub-processes to provide external services. The main process is responsible for monitoring the exit signals of the sub-processes and generating new sub-processes to handle services. This not only improves the performance of the application, but also makes Workerman more stable.
Support TCP, UDP
workerman supports TCP and UDP two transport layer protocols. You only need to change one field of the configuration to change the transport layer protocol without any changes to the business code.
Support long connections
Many times require PHP applications to maintain long connections with clients, such as chat rooms, games, etc., but traditional PHP containers (apache, nginx, php-fpm) It's very difficult to do this. Workerman makes it easy to use PHP long connections. A single PHP process can support thousands or even tens of thousands of concurrent connections, and multiple processes can support hundreds of thousands or even millions of concurrent connections.
Support various application layer protocols
The interface supports various application layer protocols, including custom protocols. The protocols supported by Workerman by default include HTTP, WebSocket, and the simple Text protocol. At the same time, Workerman provides a common protocol interface, and developers can easily develop their own protocols based on this interface.
Support high concurrency
workerman supports Libevent event polling library (Libevent extension needs to be installed). Using Libevent has excellent performance in high concurrency. If Libevent is not installed, PHP's built-in Select related System calls.
For more workerman knowledge, please pay attention to the workerman tutorial column.
The above is the detailed content of Introduction to workerman (with usage instructions). For more information, please follow other related articles on the PHP Chinese website!