Skip to content

Instantly share code, notes, and snippets.

@cboden
Created July 13, 2012 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cboden/3105006 to your computer and use it in GitHub Desktop.
Save cboden/3105006 to your computer and use it in GitHub Desktop.
Ratchet w/ php-uv
<?php
namespace Ratchet\Server;
use Ratchet\ConnectionInterface;
class UvConnection implements ConnectionInterface {
private $client;
public function __construct($client) {
$this->client = $client;
}
public function send($data) {
uv_write($this->client, $data, function($sock, $stat) {});
}
public function close() {
uv_close($this->client);
}
}
<?php
namespace Ratchet\Server;
use Ratchet\MessageComponentInterface;
class IoLibUvServer {
protected $app;
protected $tcp;
public function __construct(MessageComponentInterface $app, $port = 80, $address = '0.0.0.0') {
gc_enable();
set_time_limit(0);
ob_implicit_flush();
$this->app = $app;
$this->tcp = uv_tcp_init();
uv_tcp_bind($this->tcp, uv_ip4_addr($address, $port));
}
public function run() {
$app = $this->app;
uv_listen($this->tcp, 100, function($server) use ($app) {
$client = uv_tcp_init();
uv_accept($server, $client);
$conn = new UvConnection($client);
$app->onOpen($conn);
uv_read_start($client, function($socket, $nread, $buffer) use ($server, $app, $conn) {
$app->onMessage($conn, $buffer);
});
});
uv_run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment