示例#1
0
 public function route($event, Message $request, Message $response = null)
 {
     if ($this->upgraded) {
         parent::route($event, $request, new Response());
     } else {
         $response = new \Phasty\Server\Http\Response();
         $response->setWriteStream($request->getReadStream());
         $origin = $request->getHeader("Origin")[0];
         $location = "ws://" . $request->getHeader("Host")[0] . $request->getPath();
         $response->setCode(101, "Web Socket Protocol Handshake")->setHeader("Upgrade", "WebSocket")->setHeader("Connection", "Upgrade")->setHeader("WebSocket-Origin", $origin)->setHeader("WebSocket-Location", $location)->setHeader("WebSocket-Protocol", "sample")->on("sent", function () use($request) {
             $this->upgraded = true;
             $request->trigger("dispatched", ["nextProtocol" => $this]);
         })->send();
     }
 }
示例#2
0
 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);
 }