package com.hw.websocket.controller;

import com.alibaba.fastjson2.JSONObject;
import com.hw.websocket.operate.WebSocket;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author Wen JY
 * @description: TODO
 * @date 2023-09-14 17:07:06
 * @version: 1.0
 */
@Controller
@RequestMapping("/webSocketApi")
public class WebSocketApi {

    private final Logger logger = LoggerFactory.getLogger(WebSocketApi.class);
    @Resource
    private WebSocket webSocket;

    @PostMapping()
    @ResponseBody
    public AjaxResult push(String message){
        try{
            if(StringUtils.isEmpty(message)){
                logger.info("WebSocket Api接收数据为空");
                return AjaxResult.error("参数不允许为空");
            }
            logger.info("WebSocket Api接收数据："+message);
            webSocket.sendAllMessage(message);
            return AjaxResult.success();
        }catch (Exception ex){
            logger.error("WebSocket Api接口异常:"+ex.getMessage());
            return AjaxResult.error(ex.getMessage());
        }
    }
}
