<?php

namespace app\service;


use app\entity\gateway\ws\OutMessage;
use GatewayClient\Gateway;

class GatewayClientService
{

    /**
     * 向某个client_id对应的连接发消息
     * @param string $clientId
     * @param OutMessage $message
     * @return void
     */
    public static function sendToClient(string $clientId, OutMessage $message)
    {
        Gateway::sendToClient($clientId, $message->toJson());
    }

    /**
     * 客户端加入某个组
     * @param string $clientId
     * @param string $group
     * @return void
     */
    public static function joinGroup(string $clientId, string $group)
    {
        Gateway::joinGroup($clientId, $group);
        $group_list = $_SESSION['group_list'] ?? [];
        $group_list[] = $group;
        $_SESSION['group_list'] = $group_list;
    }

    /**
     * 将client_id与uid 绑定
     * @param string $clientId
     * @param int $uid
     * @return void
     */
    public static function bindUid(string $clientId, int $uid)
    {
        Gateway::bindUid($clientId, $uid);
    }

    public static function sendToGroup($group, OutMessage $message, $exclude_client_id = null, $raw = false)
    {
        Gateway::sendToGroup($group, $message->toJson(), $exclude_client_id, $raw);
    }
}