send() public method

Send a message.
public send ( integer $client_id, string $message, integer $opcode = self::OPCODE_TEXT_FRAME, boolean $end = true ) : boolean
$client_id integer
$message string Message.
$opcode integer Opcode.
$end boolean Whether it is the last frame of the message.
return boolean
 /**
  * 向浏览器发送数据
  * @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);
     }
 }
Example #2
0
 /**
  * 向浏览器发送数据
  * @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);
         }
     }
 }