/** * 向浏览器发送数据 * @param int $client_id * @param string $data * @return bool */ function send($client_id, $data) { /** * @var $request Swoole\Request */ $request = $this->requests[$client_id]; if ($request->isWebSocket()) { return parent::send($client_id, $data); } else { $response = new Swoole\Response(); $response->send_head('Access-Control-Allow-Origin', 'http://127.0.0.1'); $response->body = json_encode(array('success' => 1, 'text' => $data)); return $this->response($request, $response); } }
/** * 向浏览器发送数据 * @param int $session_id * @param string $data * @return bool */ function send($session_id, $data, $opcode = self::OPCODE_TEXT_FRAME, $end = true) { //WebSocket if (!$this->isCometClient($session_id)) { return parent::send($session_id, $data, $opcode, $end); } else { $session = $this->getSession($session_id); if (!$session) { $this->log("CometSesesion #{$session_id} no exists. Send failed."); return false; } else { $session->pushMessage($data); } //有等待的Request可以直接发送数据 if (isset($this->wait_requests[$session_id])) { return $this->sendMessage($session); } } }