Created
July 13, 2012 13:55
-
-
Save cboden/3105006 to your computer and use it in GitHub Desktop.
Ratchet w/ php-uv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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