/** * @param Event $event * @param ClientInterface $client * @param ServerInterface $server * @return callable */ public function __invoke(Event $event, ClientInterface $client, ServerInterface $server) { /** @var RequestHandlerInterface $requestHandler */ $requestHandler = $this->getServiceManager()->get('RequestHandler', [$this->getServiceManager()]); $callbackHandler = $client->on(['data'], $requestHandler); $requestHandler->setCallbackHandler($callbackHandler); $requestHandler->on(['request'], $this->getCallback()); $this->callPlugin('RequestHandler', [$requestHandler]); return $requestHandler; }
/** * 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_connect($client->getBackend(), $this->getBackend(), $internalCallback); return $this; }
public function send(ClientInterface $client) { $buffer = "HTTP/1.1 {$this->returnCode} {$this->returnMessage}\r\n"; $this->headers['Content-Length'] = strlen($this->body); foreach ($this->headers as $headerName => $headerValue) { $buffer .= "{$headerName}: {$headerValue}\r\n"; } $buffer .= "\r\n{$this->body}"; $response = $this; $client->write($buffer, function (ClientInterface $client) use($response) { if ($response->getHeader('Connection') == 'close') { $client->close(); } }); return $this; }