|
|||||||
Swoole - Production-Grade Async programming Framework for PHP
Время создания: 24.05.2018 15:25
Текстовые метки: php websocket swoole
Раздел: WebSocket
Запись: Velonski/mytetra-database/master/base/1527157517t2do2iypsy/text.html на raw.githubusercontent.com
|
|||||||
|
|||||||
Enable PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, Websocket services in PHP programming language without too much knowledge about non-blocking I/O programming and low-level Linux kernel.
Compared with other async programming frameworks or softwares such as Nginx, Tornado, Node.js, Swoole has the built-in async, multiple threads I/O modules. Developers can use sync or async API to write the applications. Swoole PHP network framework enhances the efficiency of R&D team, enable them to focus on the development of innovative products. Event-Driven, Asynchronous Programming For PHP Async TCP / UDP / HTTP / Websocket / HTTP2 Client/Server Side API IPv4 / IPv6 / Unixsocket / TCP/ UDP And SSL / TLS Support Fast Serializer / Unserializer High Performance, Scalable, Support C1000K Milliseconds Task Scheduler Open Source Daemonize GET STARTED 1 Installation Linux users #!/bin/bash pecl install swoole Mac users # get a list of avaiable packages brew install swoole #!/bin/bash brew install homebrew/php/php71-swoole 2 HTTP Server <?php $http = new swoole_http_server("127.0.0.1", 9501); $http->on("start", function ($server) { echo "Swoole http server is started at http://127.0.0.1:9501\n"; }); $http->on("request", function ($request, $response) { $response->header("Content-Type", "text/plain"); $response->end("Hello World\n"); }); $http->start(); 3 WebSocket Server <?php $server = new swoole_websocket_server("127.0.0.1", 9502); $server->on('open', function($server, $req) { echo "connection open: {$req->fd}\n"; }); $server->on('message', function($server, $frame) { echo "received message: {$frame->data}\n"; $server->push($frame->fd, json_encode(["hello", "world"])); }); $server->on('close', function($server, $fd) { echo "connection close: {$fd}\n"; }); $server->start(); 4 TCP Server <?php $server = new swoole_server("127.0.0.1", 9503); $server->on('connect', function ($server, $fd){ echo "connection open: {$fd}\n"; }); $server->on('receive', function ($server, $fd, $from_id, $data) { $server->send($fd, "Swoole: {$data}"); $server->close($fd); }); $server->on('close', function ($server, $fd) { echo "connection close: {$fd}\n"; }); $server->start(); 5 TCP Client <?php $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", function($cli) { $cli->send("hello world\n"); }); $client->on("receive", function($cli, $data){ echo "received: {$data}\n"; }); $client->on("error", function($cli){ echo "connect failed\n"; }); $client->on("close", function($cli){ echo "connection close\n"; }); $client->connect("127.0.0.1", 9501, 0.5); 6 Async Redis / HTTP / WebSocket Client <?php $redis = new Swoole\Redis; $redis->connect('127.0.0.1', 6379, function ($redis, $result) { $redis->set('test_key', 'value', function ($redis, $result) { $redis->get('test_key', function ($redis, $result) { var_dump($result); }); }); }); $cli = new Swoole\Http\Client('127.0.0.1', 80); $cli->setHeaders(array('User-Agent' => 'swoole-http-client')); $cli->setCookies(array('test' => 'value')); $cli->post('/dump.php', array("test" => 'abc'), function ($cli) { var_dump($cli->body); $cli->get('/index.php', function ($cli) { var_dump($cli->cookies); var_dump($cli->headers); }); }); 7 Tasks <?php $server = new swoole_server("127.0.0.1", 9502); $server->set(array('task_worker_num' => 4)); $server->on('receive', function($server, $fd, $from_id, $data) { $task_id = $server->task("Async"); echo "Dispath AsyncTask: [id=$task_id]\n"; }); $server->on('task', function ($server, $task_id, $from_id, $data) { echo "New AsyncTask[id=$task_id]\n"; $server->finish("$data -> OK"); }); $server->on('finish', function ($server, $task_id, $data) { echo "AsyncTask[$task_id] finished: {$data}\n"; }); $server->start(); |
|||||||
Так же в этом разделе:
|
|||||||
|
|||||||
|