work/extend/service/MessageService.php
vilson d0f09bae6b 增加任务隐私模式
Signed-off-by: 545522390@qq.com <liweisen825>
2019-01-22 16:04:42 +08:00

89 lines
2.5 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 service;
require_once env('app_path') . 'common/Plugins/GateWayWorker/vendor/autoload.php';
use GatewayWorker\Lib\Gateway;
/**
* 消息推送服务
* Class MessageService
* @package service
*/
class MessageService
{
/**
* MessageService constructor.
*/
public function __construct()
{
/**
*====这个步骤是必须的====
*这里填写Register服务的ip通常是运行GatewayWorker的服务器ip和端口
*注意Register服务端口在start_register.php中可以找到chat默认是1236
*这里假设GatewayClient和Register服务都在一台服务器上ip填写127.0.0.1
*注意ip不能是0.0.0.0
**/
Gateway::$registerAddress = '192.168.0.159:2346';
}
public function isUidOnline($uid)
{
return Gateway::isUidOnline($uid);
}
public function sendToAll($message, $action = '', $client_id_array = null, $exclude_client_id = null, $raw = false)
{
Gateway::sendToAll($this->messageFormat($message, $action), $client_id_array, $exclude_client_id, $raw);
}
public function sendToUid($uid, $message, $action = '')
{
Gateway::sendToUid($uid, $this->messageFormat($message, $action));
}
public function sendToClient($client_id, $message, $action = '')
{
Gateway::sendToClient($client_id, $this->messageFormat($message, $action));
}
public function sendToGroup($group, $message, $action = '')
{
Gateway::sendToGroup($group, $this->messageFormat($message, $action));
}
public function bindUid($client_id, $uid)
{
Gateway::bindUid($client_id, $uid);
}
public function joinGroup($client_id, $group)
{
if ($group) {
Gateway::joinGroup($client_id, $group);
}
}
public function messageFormat($message, $action = 'none')
{
$messageData = [
'action' => $action, //推送场景
'msg' => '', //推送内容
'title' => '消息通知',//推送标题
'data' => [],//推送数据
'uid' => 0,//推送数据
];
if (is_array($message)) {
$messageData['data'] = $message;
$messageData['msg'] = isset($message['content']) ? $message['content'] : '';
$messageData['title'] = isset($message['title']) ? $message['title'] : '';
} else {
$messageData['msg'] = $message;
}
return json_encode($messageData, JSON_UNESCAPED_UNICODE);
}
}