package com.ruoyi.web.controller.zhdd;

import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.websocket.WebSocketServer;
import com.ruoyi.framework.websocket.WebsocketConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: huangcheng
 * @Date: 2022/1/5
 * @Version V1.0
 */
@Api(value = "websocket发送消息", tags = {"websocket发送消息"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/zhdd/websocket")
public class WebSocketController extends BaseController {

    private final WebSocketServer webSocket;

    @ApiOperation("发送所有")
    @PostMapping("/sendAll")
    public AjaxResult<Void> sendAll(@RequestBody JSONObject jsonObject) {
        String message = jsonObject.getStr("message");
        JSONObject obj = new JSONObject();
        obj.set(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
        obj.set(WebsocketConst.MSG_TXT, message);
        webSocket.sendAllMessage(obj.toString());
        return AjaxResult.success();
    }
}
