concerto/sockets

基于 ReactPHP 创建基于事件的 socket 服务器和客户端的库。

维护者

详细信息

github.com/usebeagle/sockets

安装: 660

依赖者: 1

建议者: 0

安全: 0

星标: 3

关注者: 1

分支: 0

类型:concerto-component

v0.3 2014-07-01 09:09 UTC

This package is not auto-updated.

Last update: 2020-08-03 06:42:26 UTC


README

基于 ReactPHP 创建基于事件的 socket 服务器和客户端的库。

Build Status

安装

安装 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();