package cn.api.atv;

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;
import com.alibaba.fastjson2.JSONObject;

import cn.common.page.response.Result;
import cn.api.websocket.WebSocket;
import cn.facade.constant.WebsocketConst;

@RestController
@RequestMapping("webSocketApi")
public class AtvTestCtrl 
{
	
    @Autowired
    private WebSocket webSocket;
 
    @PostMapping("/sendAll")
    public Result<?> sendAll(@RequestBody JSONObject jsonObject) {
    	String message = jsonObject.getString("message");
    	JSONObject obj = new JSONObject();
    	obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
		obj.put(WebsocketConst.MSG_ID, "M0001");
		obj.put(WebsocketConst.MSG_TXT, message);
    	webSocket.sendAllMessage(obj.toJSONString());
        return Result.ok("群发");
    }

    @PostMapping("/sendUser")
    public Result<?> sendUser(@RequestBody JSONObject jsonObject) {
    	String userId = jsonObject.getString("userId");
    	String message = jsonObject.getString("message");
    	JSONObject obj = new JSONObject();
    	obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_USER);
    	obj.put(WebsocketConst.MSG_USER_ID, userId);
		obj.put(WebsocketConst.MSG_ID, "M0001");
		obj.put(WebsocketConst.MSG_TXT, message);
        webSocket.sendOneMessage(userId, obj.toJSONString());
        return Result.ok("单发");
    }
    
}