concerto/sockets

Library for creating event based socket servers and clients based on ReactPHP.

Installs: 491

Dependents: 2

Suggesters: 0

Stars: 3

Watchers: 2

Forks: 0

Type:concerto-component

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

README

Library for creating event based socket servers and clients based on ReactPHP.

Build Status

Install

The recommended way to install Sockets is through composer.

{
    "require": {
        "concerto/sockets": "0.*"
    }
}

Usage

Server

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

Client

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