/** * http请求 ->来自客户端 * @param Swoole\Request $request */ function onHttpRequest(Swoole\Request $request) { $this->swoole_server->task($request); $response = new Swoole\Response(); $response->setHttpStatus(200); $response->head['Content-Type'] = 'text/html'; $response->body = json_encode(array('ok' => 'yes', 'msg' => 'push success')); return $response; }
/** * Http请求回调 * @param Swoole\Request $request */ function onHttpRequest(Swoole\Request $request) { //新连接 if (empty($request->post['session_id'])) { if (empty($request->post['type']) or $request->post['type'] != 'connect') { goto access_deny; } $this->log("Connect [fd={$request->fd}]"); $session = $this->createNewSession(); $response = new Swoole\Response(); $response->setHeader('Access-Control-Allow-Origin', $this->origin); $response->body = json_encode(array('success' => 1, 'session_id' => $session->id)); return $response; } if (empty($request->post['type']) or empty($request->post['session_id']) or empty($this->sessions[$request->post['session_id']])) { access_deny: $response = new Swoole\Response(); $response->setHeader('Connection', 'close'); $response->setHttpStatus(403); $response->body = "<h1>Access Deny.</h1>"; return $response; } $session_id = $request->post['session_id']; $session = $this->getSession($session_id); if ($request->post['type'] == 'pub') { $response = new Swoole\Response(); $response->setHeader('Access-Control-Allow-Origin', $this->origin); $response->body = json_encode(array('success' => 1)); $this->response($request, $response); $this->log("Publish [fd={$request->fd}, session={$session_id}]"); $this->onMessage($session_id, $request->post); } elseif ($request->post['type'] == 'sub') { $this->wait_requests[$session_id] = $request; $this->fd_session_map[$request->fd] = $session_id; $this->log("Subscribe [fd={$request->fd}, session={$session_id}]"); if ($session->getMessageCount() > 0) { $this->sendMessage($session); } } else { $session = $this->createNewSession(); $response = new Swoole\Response(); $response->setHeader('Connection', 'close'); $response->setHttpStatus(404); $response->body = "<h1>Channel Not Found</h1>"; return $response; } }
/** * Do the handshake. * * @param Swoole\Request $request * @param Swoole\Response $response * @throws \Exception * @return bool */ public function doHandshake(Swoole\Request $request, Swoole\Response $response) { if (!isset($request->header['Sec-WebSocket-Key'])) { $this->log('Bad protocol implementation: it is not RFC6455.'); return false; } $key = $request->header['Sec-WebSocket-Key']; if (0 === preg_match('#^[+/0-9A-Za-z]{21}[AQgw]==$#', $key) || 16 !== strlen(base64_decode($key))) { $this->log('Header Sec-WebSocket-Key: $key is illegal.'); return false; } /** * @TODO * ? Origin; * ? Sec-WebSocket-Protocol; * ? Sec-WebSocket-Extensions. */ $response->setHttpStatus(101); $response->addHeaders(array('Upgrade' => 'websocket', 'Connection' => 'Upgrade', 'Sec-WebSocket-Accept' => base64_encode(sha1($key . static::GUID, true)), 'Sec-WebSocket-Version' => self::WEBSOCKET_VERSION)); return true; }
/** * 动态请求 * @param $request * @param $response * @return unknown_type */ function processDynamic(Swoole\Request $request, Swoole\Response $response) { $path = $this->document_root . '/' . $request->meta['path']; if (is_file($path)) { $request->setGlobal(); $response->head['Content-Type'] = 'text/html'; ob_start(); try { include $path; $response->body = ob_get_contents(); } catch (\Exception $e) { $response->setHttpStatus(500); $response->body = $e->getMessage() . '!<br /><h1>' . self::SOFTWARE . '</h1>'; } ob_end_clean(); } else { $this->httpError(404, $response, "页面不存在({$request->meta['path']})!"); } }
/** * 动态请求 * @param $request * @param $response * @return unknown_type */ function processDynamic(Swoole\Request $request, Swoole\Response $response) { $request->setGlobal(); $response->head['Content-Type'] = 'text/html'; ob_start(); try { $this->application->bootstrap(); $request_uri = $request->meta['path']; $this->application->getDispatcher()->dispatch(new \Yaf_Request_Http($request_uri)); $response->body = ob_get_contents(); } catch (\Exception $e) { $response->setHttpStatus(500); $response->body = $e->getMessage() . '!<br /><h1>' . self::SOFTWARE . '</h1>'; } ob_end_clean(); }