concerto / sockets
基于 ReactPHP 创建基于事件的 socket 服务器和客户端的库。
v0.3
2014-07-01 09:09 UTC
Requires
- php: >=5.4.0
- evenement/evenement: 2.0.*
- react/socket: 0.4.*
- react/socket-client: 0.4.*
This package is not auto-updated.
Last update: 2020-08-03 06:42:26 UTC
README
基于 ReactPHP 创建基于事件的 socket 服务器和客户端的库。
安装
安装 Sockets 的推荐方法是 通过 composer。
{ "require": { "concerto/sockets": "0.*" } }
用法
服务器
use Concerto\Sockets\Server; use React\EventLoop\Factory as EventLoopFactory; $loop = EventLoopFactory::create(); $server = new Server($loop, 'tcp://127.0.0.1:1234'); $server->on('connect', function($conn) { $conn->write("Hello client!\n"); $conn->on('data', function ($data) use ($conn) { $conn->close(); }); }); $server->listen(); $loop->run();
客户端
use Concerto\Sockets\Client; use React\EventLoop\Factory as EventLoopFactory; use React\Stream\Stream; $loop = EventLoopFactory::create(); $client = new Client($loop, 'tcp://127.0.0.1:1234'); $client->on('connect', function($conn) use ($loop) { $conn->pipe(new Stream(STDOUT, $loop)); $conn->write("Hello World!\n"); }); $client->connect(); $loop->run();