yuanling / workerman
一个异步事件驱动的PHP框架,用于轻松构建快速、可扩展的网络应用程序。
v4.0.17
2020-11-27 04:14 UTC
Requires
- php: >=5.3
Suggests
- ext-event: For better performance.
- dev-master
- v4.0.17
- v4.0.16
- v4.0.15
- v4.0.14
- v4.0.13
- v4.0.12
- v4.0.11
- v4.0.10
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- 3.x-dev
- v3.5.31
- v3.5.30
- v3.5.29
- v3.5.28
- v3.5.27
- v3.5.26
- v3.5.25
- v3.5.24
- v3.5.23
- v3.5.22
- v3.5.21
- v3.5.20
- v3.5.19
- v3.5.18
- v3.5.17
- v3.5.16
- v3.5.15
- v3.5.14
- v3.5.13
- v3.5.12
- v3.5.11
- v3.5.10
- v3.5.9
- v3.5.8
- v3.5.7
- v3.5.6
- v3.5.5
- v3.5.4
- v3.5.3
- v3.5.2
- v3.5.1
- v3.5.0
- v3.4.7
- v3.4.6
- v3.4.5
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.9
- v3.3.8
- v3.3.7
- v3.3.6
- v3.3.5
- v3.3.4
- v3.3.3
- v3.3.1
- v3.3.0
- v3.2.9
- v3.2.8
- v3.2.7
- v3.2.6
- v3.2.5
- v3.2.2
- v3.2.0
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v2.1.6
This package is not auto-updated.
Last update: 2024-10-03 18:32:23 UTC
README
这是什么
Workerman是一个高性能的异步事件驱动PHP框架,可以构建快速且可扩展的网络应用程序。Workerman支持HTTP、Websocket、SSL和其他自定义协议。Workerman支持事件扩展。
要求
PHP 5.3或更高版本
POSIX兼容的操作系统(Linux、OSX、BSD)
需要POSIX和PCNTL扩展
推荐使用事件扩展以获得更好的性能
安装
composer require workerman/workerman
基本用法
一个WebSocket服务器
<?php use Workerman\Worker; require_once __DIR__ . '/vendor/autoload.php'; // Create a Websocket server $ws_worker = new Worker('websocket://0.0.0.0:2346'); // 4 processes $ws_worker->count = 4; // Emitted when new connection come $ws_worker->onConnect = function ($connection) { echo "New connection\n"; }; // Emitted when data received $ws_worker->onMessage = function ($connection, $data) { // Send hello $data $connection->send('Hello ' . $data); }; // Emitted when connection closed $ws_worker->onClose = function ($connection) { echo "Connection closed\n"; }; // Run worker Worker::runAll();
一个HTTP服务器
use Workerman\Worker; require_once __DIR__ . '/vendor/autoload.php'; // #### http worker #### $http_worker = new Worker('http://0.0.0.0:2345'); // 4 processes $http_worker->count = 4; // Emitted when data received $http_worker->onMessage = function ($connection, $request) { //$request->get(); //$request->post(); //$request->header(); //$request->cookie(); //$requset->session(); //$request->uri(); //$request->path(); //$request->method(); // Send data to client $connection->send("Hello World"); }; // Run all workers Worker::runAll();
一个TCP服务器
use Workerman\Worker; require_once __DIR__ . '/vendor/autoload.php'; // #### create socket and listen 1234 port #### $tcp_worker = new Worker('tcp://0.0.0.0:1234'); // 4 processes $tcp_worker->count = 4; // Emitted when new connection come $tcp_worker->onConnect = function ($connection) { echo "New Connection\n"; }; // Emitted when data received $tcp_worker->onMessage = function ($connection, $data) { // Send data to client $connection->send("Hello $data \n"); }; // Emitted when connection is closed $tcp_worker->onClose = function ($connection) { echo "Connection closed\n"; }; Worker::runAll();
启用SSL
<?php use Workerman\Worker; require_once __DIR__ . '/vendor/autoload.php'; // SSL context. $context = array( 'ssl' => array( 'local_cert' => '/your/path/of/server.pem', 'local_pk' => '/your/path/of/server.key', 'verify_peer' => false, ) ); // Create a Websocket server with ssl context. $ws_worker = new Worker('websocket://0.0.0.0:2346', $context); // Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://). // The similar approaches for Https etc. $ws_worker->transport = 'ssl'; $ws_worker->onMessage = function ($connection, $data) { // Send hello $data $connection->send('Hello ' . $data); }; Worker::runAll();
自定义协议
协议/MyTextProtocol.php
namespace Protocols; /** * User defined protocol * Format Text+"\n" */ class MyTextProtocol { public static function input($recv_buffer) { // Find the position of the first occurrence of "\n" $pos = strpos($recv_buffer, "\n"); // Not a complete package. Return 0 because the length of package can not be calculated if ($pos === false) { return 0; } // Return length of the package return $pos+1; } public static function decode($recv_buffer) { return trim($recv_buffer); } public static function encode($data) { return $data . "\n"; } }
use Workerman\Worker; require_once __DIR__ . '/vendor/autoload.php'; // #### MyTextProtocol worker #### $text_worker = new Worker('MyTextProtocol://0.0.0.0:5678'); $text_worker->onConnect = function ($connection) { echo "New connection\n"; }; $text_worker->onMessage = function ($connection, $data) { // Send data to client $connection->send("Hello world\n"); }; $text_worker->onClose = function ($connection) { echo "Connection closed\n"; }; // Run all workers Worker::runAll();
定时器
use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; $task = new Worker(); $task->onWorkerStart = function ($task) { // 2.5 seconds $time_interval = 2.5; $timer_id = Timer::add($time_interval, function () { echo "Timer run\n"; }); }; // Run all workers Worker::runAll();
AsyncTcpConnection (tcp/ws/text/frame等...)
use Workerman\Worker; use Workerman\Connection\AsyncTcpConnection; require_once __DIR__ . '/vendor/autoload.php'; $worker = new Worker(); $worker->onWorkerStart = function () { // Websocket protocol for client. $ws_connection = new AsyncTcpConnection('ws://echo.websocket.org:80'); $ws_connection->onConnect = function ($connection) { $connection->send('Hello'); }; $ws_connection->onMessage = function ($connection, $data) { echo "Recv: $data\n"; }; $ws_connection->onError = function ($connection, $code, $msg) { echo "Error: $msg\n"; }; $ws_connection->onClose = function ($connection) { echo "Connection closed\n"; }; $ws_connection->connect(); }; Worker::runAll();
可用命令
php start.php start
php start.php start -d
php start.php status
php start.php connections
php start.php stop
php start.php restart
php start.php reload
文档
中文文档: http://doc.workerman.net
文档:https://github.com/walkor/workerman-manual
基准测试
https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=plaintext&l=zik073-1r
与其他Workerman相关的链接
PHPSocket.IO
php-socks5
php-http-proxy
捐赠
许可证
Workerman遵循MIT许可证。