/** * Connects a client to the socket * * @param ClientInterface $client * @param callable $callback * @return SocketInterface */ public function connect(ClientInterface $client, callable $callback) { $internalCallback = function ($resource, $status) use($callback, $client) { if (!$status) { $client->emit(new Event('error'), [$resource, new ConnectionError('Could not open connection.', $status)]); return; } $client->on(['data'], $callback); }; \uv_tcp_connect6($client->getBackend(), $this->getBackend(), $internalCallback); return $this; }
$c = uv_tcp_init(); uv_tcp_connect6($c, uv_ip6_addr('::1', 9999), function ($client, $stat) { if ($stat == 0) { $request = new ProtocolCached_Request(); $type = getRequestType($_SERVER['argv'][1]); $request->setType($type); if ($type == ProtocolCached_Request_RequestType::SET) { $set = new ProtocolCached_SetRequest(); $set->setKey($_SERVER['argv'][2]); $set->setValue($_SERVER['argv'][3]); $request->setSet($set); } else { if ($type == ProtocolCached_Request_RequestType::GET) { $set = new ProtocolCached_GetRequest(); $set->setKey($_SERVER['argv'][2]); $request->setGet($set); } } uv_write($client, $request->serializeToString(), function ($socket, $stat) use($type) { if ($type == ProtocolCached_Request_RequestType::GET) { uv_read_start($socket, function ($socket, $nread, $buffer) use($type) { var_dump(ProtocolCached_Response::parseFromString($buffer)); uv_close($socket); }); } else { uv_close($socket); } }); } }); uv_run();
<?php $tcp = uv_tcp_init(); uv_tcp_bind6($tcp, uv_ip6_addr('::1', 9999)); uv_listen($tcp, 100, function ($server) { $client = uv_tcp_init(); uv_accept($server, $client); var_dump(uv_tcp_getsockname($server)); uv_read_start($client, function ($socket, $nread, $buffer) use($server) { var_dump($buffer); uv_close($socket); uv_close($server); }); }); $c = uv_tcp_init(); uv_tcp_connect6($c, uv_ip6_addr('::1', 9999), function ($client, $stat) { if ($stat == 0) { uv_write($client, "Hello", function ($socket, $stat) { uv_close($socket); }); } }); uv_run();