<?php
/*
 * description：
 * author：wh
 * email：
 * createTime：{2024/4/5} {19:51} 
 */

namespace app\api\controller;


use app\api\logic\StatisticsLogic;
use app\common\model\TabConf;
use GatewayWorker\Lib\Gateway;
use think\Db;
use wanghua\general_utility_tools_php\Date;
use wanghua\general_utility_tools_php\tool\Tools;


/**
 * socket 主动推送
 *
 * Class Wsspush
 * @package app\api\controller
 */
class Wsspush extends BaseWssApi
{

    public function index()
    {

        $socketTaskId = input('socketTaskId');
        Gateway::sendToClient($socketTaskId,json_encode(Tools::set_ok('向指定客户端发送信息。',$socketTaskId),JSON_UNESCAPED_UNICODE));


    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    /**
     * desc：向客户端推送消息
     *
     * 接收一维数组表单
     *
     * /api/wsspush/pushMessageToClient
     *
     * author：wh
     */
    function pushMessageToClient(){
        $socketTaskId = input('clientid');
        if(empty($socketTaskId)){
            return json(Tools::set_fail('客户端id必须'));
        }

        $action = input('action');
        if(empty($action)){
            return json(Tools::set_fail('ACTION MUST'));
        }

        $msg = input('msg');
        if(empty($msg)){
            return json(Tools::set_fail('MSG MUST'));
        }
        $all_params = [];
        foreach (input() as $key=>$item){
            if(in_array($key, ['clientid','action','msg'])){
                continue;
            }
            $all_params[$key] = $item;
        }

        //json_encode(Tools::set_ok('向指定客户端发送信息。',$socketTaskId),JSON_UNESCAPED_UNICODE);
        $json = self::json_wss($action,$msg,$all_params);

        Gateway::sendToClient($socketTaskId, $json);
        return json(Tools::set_ok('向指定客户端发送信息成功。'));
    }
}