package org.springblade.modules.sse.controller;
|
|
import lombok.AllArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springblade.modules.sse.server.SSEServer;
|
import org.springblade.modules.sse.vo.SseVO;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
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;
|
|
@Slf4j
|
@RestController
|
@CrossOrigin
|
@RequestMapping("/sse/sse")
|
@AllArgsConstructor
|
public class SSEController {
|
|
/**
|
* 建立连接
|
* @param sse
|
* @return
|
*/
|
@GetMapping("/connect")
|
public SseEmitter connect(SseVO sse){
|
String userId = sse.getType() + ":" + sse.getUserId();
|
return SSEServer.connect(userId);
|
}
|
|
/**
|
* 断开连接
|
* @param sse
|
* @return
|
*/
|
@GetMapping("/disconnect")
|
public void disconnect(SseVO sse){
|
String userId = sse.getType() + ":" + sse.getUserId();
|
SSEServer.removeUser(userId);
|
}
|
}
|