package com.cyksj.web.controller.sse;


import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.server.SseEmitterServer;
import com.cyksj.web.util.StpUserUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@RestController
@RequestMapping("/sse")
@Log4j2
public class SseController {

    /**
     * 用于创建连接
     */
    @GetMapping("/connect")
    public SseEmitter connect() {
        long userId = StpUserUtil.getLoginIdAsLong();
        return SseEmitterServer.connect(userId);
    }

    /**
     * 关闭连接
     */
    @GetMapping("/close")
    public Result<String> close() {
        long userId = StpUserUtil.getLoginIdAsLong();
        SseEmitterServer.removeUser(userId);
        return GatewayResponse.SUCCESS.newBuilder().toResult("连接关闭");
    }
}
