Files
mier-php/application/api/model/WebSocketPush.php
2025-08-11 10:22:05 +08:00

222 lines
7.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\model;
use think\facade\Log;
class WebSocketPush
{
public $websocket_api_address;
public $websocket_api_key;
//声网
public $customer_key; //声网key
public $customer_secret; //声网secret
public $app_id; //声网appid
public $app_certificate;
public $channel_name = '123456789'; //频道
public $header_arr = []; //请求头
public $base_url = 'https://api.agora.io/dev/v2/project/';
// public $base_url = 'https://api.agora.io/dev/v1/projects';
public $request_url;
public $uid;
public $token;
public $credentials;
/*
200 通用数据
房间PK
220 推送PK当前结果数据
300
用户进入房间推送消息
301
房间麦位信息
302
房间赠送礼物推送数据
303
开宝箱房间播报数据
304
开宝箱全服播报数据
400
房间相亲数据
355
挂件推送
369
清理房间用户推送
3001
竞拍前三名信息
3002
签约成功推送
3005
绑定关系成功推送
3010
自己签约的艺人被拍卖
*/
public function __construct()
{
$config = model('admin/Config')->get_uncache_system_config();
// $this->websocket_api_address = $config['websocket_api_address'];
// $this->websocket_api_key = $config['websocket_api_key'];
// $this->uid = 99999999966;
// $this->token = $token;
$this->app_id = $config['agora_app_id'];
$this->customer_key = $config['agora_app_key'];
$this->customer_secret = $config['agora_app_secret'];
// $this->app_certificate = $config['agora_app_certificate'];
// $this->token = model('agora')->agora_rtm_token_info($this->uid);
$this->credentials = $this->customer_key . ':' . $this->customer_secret;
// dump($this->customer_key);
// dump($this->customer_secret);die;
$base64Credentials = base64_encode($this->credentials);
$this->header_arr = [
// 'x-agora-uid: ' . $this->uid,
// "x-agora-token: " . $this->token,
'Authorization: Basic ' . $base64Credentials,
'Content-Type: application/json'
];
}
//发送消息给个人
// public function send_to_one($uid, $content)
// {
// $post_data = [];
// // $post_data['action'] = 'send_to_one';
// // $post_data['key'] = $this->websocket_api_key;
// if (is_array($content)) {
// $post_data['content'] = json_encode($content);
// } else {
// $post_data['content'] = $content;
// }
// $post_data['uid'] = $uid;
// $content = $this->curlPost($this->websocket_api_address, $post_data);
// return $content;
// }
//发送消息给个人
public function send_to_one($uid, $content)
{
$post_data = [];
// $post_data['action'] = 'send_to_one';
// $post_data['key'] = $this->websocket_api_key;
if (is_array($content)) {
// $post_data['content'] = json_encode($content);
$post_data['content'] = $content;
} else {
$post_data['content'] = $content;
}
// $post_data['uid'] = $uid;
// $content = $this->curlPost($this->websocket_api_address, $post_data);
$this->request_url = $this->base_url . $this->app_id . '/rtm/users/8889987154254/peer_messages';
$data['payload'] = json_encode($post_data);
$data['destination'] = strval($uid);
$data['enable_historical_messaging'] = false;
$data = json_encode($data);
$content = $this->curlPost($this->request_url, $data, $this->header_arr);
return $content;
}
//发送消息到指定房间
public function send_to_group($rid, $content)
{
$post_data = [];
// $post_data['action'] = 'send_to_group';
// $post_data['key'] = $this->websocket_api_key;
// if (is_array($content)) {
// $post_data['content'] = json_encode($content);
// } else {
// $post_data['content'] = $content;
// }
$post_data['content'] = $content;
$post_data['hid'] = $rid;
$data['channel_name'] = strval($rid);
$data['payload'] = json_encode($post_data);
$data['enable_historical_messaging'] = false;
$data = json_encode($data);
// $this->request_url = $this->base_url . $this->app_id . '/rtm/users/' . $this->uid . '/channel_messages';
// $this->request_url = $this->base_url;
$this->request_url = $this->base_url . $this->app_id . '/rtm/users/8889987154254/channel_messages';
// dump($data);
$content = $this->curlPost($this->request_url, $data ,$this->header_arr);
// dump($content);die;
return $content;
}
//发送群体消息
public function send_to_all($content)
{
$post_data = [];
// $post_data['action'] = 'send_to_group';
// $post_data['key'] = $this->websocket_api_key;
// if (is_array($content)) {
// $post_data['content'] = json_encode($content);
// } else {
// $post_data['content'] = $content;
// }
$post_data['content'] = $content;
$data['channel_name'] = $this->channel_name;
$data['payload'] = json_encode($post_data);
$data['enable_historical_messaging'] = false;
$data = json_encode($data);
$this->request_url = $this->base_url . $this->app_id . '/rtm/users/8889987154254/channel_messages';
// dump($this->request_url);die;
$content = $this->curlPost($this->request_url, $data,$this->header_arr);
// dump($content);die;
return $content;
}
public function getallclients()
{
$post_data = [];
$post_data['action'] = 'getallclients';
$post_data['key'] = $this->websocket_api_key;
$content = $this->curlPost($this->websocket_api_address, $post_data);
return $content;
}
//参数1访问的URL参数2post数据(不填则为GET)参数3提交的$cookies,参数4是否返回$cookies
// public function myCurl($url, $post_data = array(), $header = array(), $cookie = "")
// {
// // $url = 'http://paysystem.local.com/index/apip/cc';
// // dump($url);
// curlPost($url)
// }
public function curlPost($url, $data = [], $header = [])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_TIMEOUT , 0);
curl_setopt($curl, CURLOPT_MAXREDIRS , 10);
curl_setopt($curl, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_POST, 1);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if (!empty($header)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}