Gateway::sendToAll PHP Метод

sendToAll() публичный статический Метод

向所有客户端(或者client_id_array指定的客户端)广播消息
public static sendToAll ( string $message, array $client_id_array = null )
$message string 向客户端发送的消息(可以是二进制数据)
$client_id_array array 客户端id数组
    public static function sendToAll($message, $client_id_array = null)
    {
        $gateway_data = GatewayProtocol::$empty;
        $gateway_data['cmd'] = GatewayProtocol::CMD_SEND_TO_ALL;
        $gateway_data['body'] = $message;
        if ($client_id_array) {
            $params = array_merge(array('N*'), $client_id_array);
            $gateway_data['ext_data'] = call_user_func_array('pack', $params);
        } elseif (empty($client_id_array) && is_array($client_id_array)) {
            return;
        }
        // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
        if (self::$businessWorker) {
            foreach (self::$businessWorker->gatewayConnections as $gateway_connection) {
                $gateway_connection->send($gateway_data);
            }
        } else {
            $all_addresses = Store::instance('gateway')->get('GLOBAL_GATEWAY_ADDRESS');
            if (!is_array($all_addresses)) {
                throw new \Exception('bad gateway addresses ' . var_export($all_addresses, true));
            }
            foreach ($all_addresses as $address) {
                self::sendToGateway($address, $gateway_data);
            }
        }
    }

Usage Example

Пример #1
0
 public function pushMessage($toUids, $data)
 {
     $clients = $this->getClientByUser($toUids);
     if ($clients) {
         if (!class_exists('Gateway', false)) {
             require ADDON_PATH . '/library/GatewayClient/Gateway.php';
         }
         foreach ($data as &$rs) {
             $rs['message_id'] = (int) $rs['message_id'];
             $rs['from_uid'] = (int) $rs['from_uid'];
             $rs['room_id'] = (int) $rs['list_id'];
             $rs['mtime'] = (int) $rs['mtime'];
             $rs['from_uname'] = (string) getUserName($rs['from_uid']);
             if (isset($rs['attach_id'])) {
                 $rs['attach_id'] = @desencrypt($rs['attach_id'], C('SECURE_CODE'));
             }
             $rs['content'] = $this->htmlDecode($rs['content']);
             if (isset($rs['location'])) {
                 $rs['location'] = $this->htmlDecode($rs['location']);
             }
             if (isset($rs['title'])) {
                 $rs['title'] = $this->htmlDecode($rs['title']);
             }
             unset($rs['list_id']);
         }
         $data = json_encode(array('type' => 'push_message', 'result' => array('from' => 'web', 'length' => count($data), 'list' => $data), 'status' => 0, 'msg' => ''));
         Gateway::sendToAll($data, $clients);
     }
 }
All Usage Examples Of Gateway::sendToAll