package com.yfd.platform.system.controller;

import com.yfd.platform.component.ServerSendEventServer;
import com.yfd.platform.component.WebSocketServer;
import com.yfd.platform.system.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;


@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/sse")
@Api(tags = "SSE推送服务")
public class SSEController {

    @Resource
    private IMessageService messageService;

    @GetMapping("/connect/{token}")
    @ApiOperation("建立连接")
    public SseEmitter connect(@PathVariable String token) {
        SseEmitter connect = ServerSendEventServer.connect(token);
        return connect;
    }

    @GetMapping("/sendmsg")
    @ApiOperation("发送消息")
    public void sendMessage(String token, String message) throws InterruptedException {

        ServerSendEventServer.sendMessage(token, message);
    }

    @GetMapping("/sendgroupmsg")
    @ApiOperation("多人发送消息")
    public void sendgroupmsg(String groupid, String message) throws InterruptedException {
        WebSocketServer.sendInfo(groupid, message);
    }

    @GetMapping("/disconnect/{token}")
    @ApiOperation("关闭连接")
    public void disconnect(@PathVariable String token) throws InterruptedException {
        ServerSendEventServer.removeUser(token);
    }
}
