public function route($event, Message $request, Message $response = null) { if ($this->upgraded) { switch ($request->getOpCode()) { case Request::OPCODE_CTRL_CLOSE: $this->sendClose($request); break; case Request::OPCODE_CTRL_PING: $this->sendPong($request); break; case Request::OPCODE_CTRL_PONG: break; case Request::OPCODE_NCTRL_TEXT: case Request::OPCODE_NCTRL_BIN: parent::route($event, $request, new Response()); break; default: throw new \Exception("Unknown opcode " . var_export($request->getOpCode(), 1)); } } else { $response = new \Phasty\Server\Http\Response(); $response->setWriteStream($request->getReadStream()); $hash = $this->getAcceptHash($request->getHeader("Sec-WebSocket-Key")[0]); $origin = $request->getHeader("Origin")[0]; $location = "ws://" . $request->getHeader("Host")[0] . $request->getPath(); $response->setCode(101)->setHeader("Upgrade", "websocket")->setHeader("Connection", "Upgrade")->setHeader("Origin", $origin)->setHeader("Sec-WebSocket-Accept", $hash); if ($request->hasHeader("Sec-WebSocket-Protocol")) { $request->setHeader("Sec-WebSocket-Protocol", "chat"); } $response->on("sent", function () use($request) { $this->upgraded = true; $request->trigger("dispatched", ["nextProtocol" => $this]); })->send(); } }
public function route($event, Message $request, Message $response = null) { $response = $response instanceof Message ? $response : new Response(); $response->setWriteStream($request->getReadStream()); $response->on("ready", function ($event, $response) use($request) { $response->on("sent", [$request, "dispatched"])->send(); }); $this->server->trigger("request", (object) compact("request", "response")); }
public function route($event, Message $request, Message $response = null) { if ($request->hasHeader("Upgrade") && $request->hasHeader("Connection") && mb_strtolower($request->getHeader("Upgrade")[0]) == "websocket" && mb_strtolower($request->getHeader("Connection")[0]) == "upgrade") { foreach (["ProtocolRfc6455", "ProtocolHixie75"] as $protocol) { $protocol = "\\Phasty\\Server\\WebSocket\\{$protocol}"; if (!$protocol::match($request)) { continue; } $protocol = new $protocol(); $protocol->route($event, $request); return; } } parent::route($event, $request); }
public function care(\Phasty\Server\Message $request, \Phasty\Server\Message $response) { $response->setCode(400); $response->send(); }