new_naweigete/crmeb/services/workerman/ChannelService.php
2025-03-12 10:47:34 +08:00

87 lines
2.1 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
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\services\workerman;
use Channel\Client;
class ChannelService
{
/**
* @var Client
*/
protected $channel;
/**
* @var
*/
protected $trigger = 'crmeb';
/**
* @var ChannelService
*/
protected static $instance;
public function __construct()
{
self::connet();
}
public static function instance()
{
if (is_null(self::$instance))
self::$instance = new self();
return self::$instance;
}
public static function connet()
{
$config = config('workerman.channel');
Client::connect($config['ip'], $config['port']);
}
/**
* @param string $name
* @return $this
*/
public function setTrigger(string $name)
{
$this->trigger = $name;
return $this;
}
/**
* 发送消息
* @param string $type 类型
* @param array|null $data 数据
* @param array|null $ids 用户 id,不传为全部用户
*/
public function send(string $type, ?array $data = null, ?array $ids = null)
{
$res = compact('type');
if (!is_null($data))
$res['data'] = $data;
if (!is_null($ids) && count($ids))
$res['ids'] = $ids;
$this->trigger($this->trigger, $res);
$this->trigger = 'crmeb';
}
public function trigger(string $type, ?array $data = null)
{
Client::publish($type, $data);
}
}