public function onRequest(Request $request, Socket $socket) { $data = ''; $body = $request->getBody(); while ($body->isReadable()) { $data .= (yield $body->read()); } $requestData = json_decode($data, true); if (isset($requestData['type'])) { $type = $requestData['type']; if ($type === 'confirmation') { $confirmToken = (yield from $this->confirmToken($requestData['group_id'])); return yield from $this->ok($confirmToken); } if (!empty($this->listeners[$type])) { /** @var Coroutine\Coroutine[] $coroutines */ $coroutines = []; foreach ($this->listeners[$type] as $listener) { $coroutines[] = $listener($requestData); } Awaitable\all($coroutines)->wait(); } } return yield from $this->ok(); }
public function handle(Request $request, array $args) : \Generator { $article = $this->app->getArticleStore()->getBySlug(substr($request->getUri()->getPath(), 1)); $html = $this->app->getRenderer()->render('article', ['article' => $article]); $sink = new MemorySink(); yield from $sink->end($html); return new BasicResponse(200, ['Content-Type' => 'text/html', 'Content-Length' => $sink->getLength()], $sink); }
public function onRequest(Request $request, Socket $socket) { $path = $request->getUri()->getPath(); if ($path == "/") { return yield from $this->onHttp($request, $socket); } if ($path == "/socket") { return yield from $this->onSocket($request, $socket); } return yield from $this->onError(404, $socket); }