/**
  * 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;
 }
 /**
  * 定时器,检查某些连接是否已超过最大时间
  * @param $serv
  * @param $interval
  */
 function onTimer($serv, $interval)
 {
     $now = time();
     //echo "timer $interval\n";
     foreach ($this->requests as $request) {
         if ($request->time < $now - $this->request_timeout) {
             $response = new Swoole\Response();
             $response->send_head('Access-Control-Allow-Origin', 'http://127.0.0.1');
             $response->body = json_encode(array('success' => 0, 'text' => 'timeout'));
             $this->response($request, $response);
         }
     }
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * 定时器,检查某些连接是否已超过最大时间
  * @param $timerId
  */
 function onTimer($timerId)
 {
     $now = time();
     //echo "timer $interval\n";
     foreach ($this->wait_requests as $id => $request) {
         if ($request->time < $now - $this->request_timeout) {
             $response = new Swoole\Response();
             $response->setHeader('Access-Control-Allow-Origin', $this->origin);
             $response->body = json_encode(array('success' => 0, 'text' => 'timeout'));
             $this->response($request, $response);
             unset($this->wait_requests[$id]);
         }
     }
 }
Example #5
0
 /**
  * 动态请求
  * @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']})!");
     }
 }
Example #6
0
 /**
  * http请求 ->来自客户
  * todo 检验消息之后通知客服 然后关闭连接
  * @param Swoole\Request $request
  */
 function onHttpRequest(Swoole\Request $request)
 {
     $this->log("Http connection  is connected");
     $response = new Swoole\Response();
     $response->setHeader('Content-Type', 'image/jpeg');
     return $response;
 }
 /**
  * 动态请求
  * @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();
 }
 /**
  * 静态请求
  * @param $request
  * @param $response
  * @return unknown_type
  */
 function process_static($request, Swoole\Response $response)
 {
     $path = $this->document_root . '/' . $request->meta['path'];
     if (is_file($path)) {
         $read_file = true;
         if ($this->expire) {
             $expire = intval($this->config['server']['expire_time']);
             $fstat = stat($path);
             //过期控制信息
             if (isset($request->head['If-Modified-Since'])) {
                 $lastModifiedSince = strtotime($request->head['If-Modified-Since']);
                 if ($lastModifiedSince and $fstat['mtime'] <= $lastModifiedSince) {
                     //不需要读文件了
                     $read_file = false;
                     $response->send_http_status(304);
                 }
             } else {
                 $response->head['Cache-Control'] = "max-age={$expire}";
                 $response->head['Pragma'] = "max-age={$expire}";
                 $response->head['Last-Modified'] = date(self::DATE_FORMAT_HTTP, $fstat['mtime']);
                 $response->head['Expires'] = "max-age={$expire}";
             }
         }
         $ext_name = \Upload::file_ext($request->meta['path']);
         if ($read_file) {
             $response->head['Content-Type'] = $this->mime_types[$ext_name];
             $response->body = file_get_contents($path);
         }
         return true;
     } else {
         return false;
     }
 }
Example #9
0
 /**
 * 发送响应
 * @param $client_id
 * @param \Swoole\Response $response
 * @return unknown_type
 */
 function response($client_id, $response)
 {
     if (!isset($response->head['Date'])) {
         $response->head['Date'] = gmdate("D, d M Y H:i:s T");
     }
     if (!isset($response->head['Server'])) {
         $response->head['Server'] = self::SOFTWARE;
     }
     if (!isset($response->head['KeepAlive'])) {
         $response->head['KeepAlive'] = 'off';
     }
     if (!isset($response->head['Connection'])) {
         $response->head['Connection'] = 'close';
     }
     if (!isset($response->head['Content-Length'])) {
         $response->head['Content-Length'] = strlen($response->body);
     }
     $out = $response->head();
     $out .= $response->body;
     $this->server->send($client_id, $out);
 }