springboot使用SSE

1、pom文件

XML 复制代码
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

2、前端代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Springboot集成SSE </title>
</head>
<script>
    let source = null;
    const clientId = new Date().getTime();
    if (!!window.EventSource) {

        source = new EventSource('http://127.0.0.1:8080/sse/subscribe?id=' + clientId);

    	//建立连接
        source.onopen = function (event) {
            setMessageInnerHTML("建立连接" + event);
        }
        //接收数据
        source.onmessage = function (event) {
            setMessageInnerHTML(event.data);
        }
        //错误监听
        source.onerror = function (event) {
            if (event.readyState === EventSource.CLOSED) {
                setMessageInnerHTML("连接关闭");
            } else {
                console.log(event);
            }
        }
    } else {
        setMessageInnerHTML("浏览器不支持SSE");
    }

    window.onbeforeunload = function () {
        close();
    };

    // 关闭
    function close() {
        source.close();
        const httpRequest = new XMLHttpRequest();
        httpRequest.open('GET', '/sse/over/?clientId=' + clientId, true);
        httpRequest.send();
        console.log("close");
    }

    // 显示消息
    function setMessageInnerHTML(innerHTML) {
        document.getElementById('text').innerHTML += innerHTML + '<br/>';
    }
</script>
<body>
<button onclick="close()">关闭连接</button>
<div id="text"></div>
</body>
</html>

3、后端代码

1、订阅
java 复制代码
private static Map<String, SseEmitter> cache = new ConcurrentHashMap<>();
	@GetMapping(path = "subscribe", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
	public SseEmitter subscribe(@RequestParam(name = "id", required = false) String id) throws IOException {
		// 超时时间设置
		SseEmitter sseEmitter = new SseEmitter(0L);
		
		cache.put(id, sseEmitter);
		//结束连接
		sseEmitter.onCompletion(() -> {
			log.info("结束连接:{}", id);
			cache.remove(id);
		});
		//连接异常
		sseEmitter.onError(throwable -> {
			log.info("连接异常:{}", id);
			cache.remove(id);
		});
		//连接超时
		sseEmitter.onTimeout(() -> {
			log.info("连接超时:{}", id);
			cache.remove(id);
		});
		return sseEmitter;
	}
2、推送
java 复制代码
@GetMapping(path = "push/{userId}")
	public String push(@PathVariable String userId,@RequestBody Map<String,Object> param) throws IOException {
		try {
			SseEmitter sseEmitter = cache.get("123");
			if (sseEmitter != null) {
				sseEmitter.send(SseEmitter.event().name("msg").data("后端发送消息:" + param));
			}
		} catch (IOException e) {
			log.error("用户[{}]推送异常:{}", userId, e.getMessage());
			cache.remove(userId);
		}
		return "over";
	}
3、关闭
java 复制代码
@GetMapping(path = "over")
	public String over(@RequestParam(name = "id", required = false) String id) {
		SseEmitter sseEmitter = cache.get(id);
		if (sseEmitter != null) {
			sseEmitter.complete();
			cache.remove(id);
		}
		return "over";
	}
相关推荐
非 白4 分钟前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼10 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
m0_7482336411 分钟前
RabbitMQ 进阶
android·前端·后端
羱滒13 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
m0_7482386321 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
桦说编程22 分钟前
【硬核总结】如何轻松实现只计算一次、惰性求值?良性竞争条件的广泛使用可能超过你的想象!String实际上是可变的?
后端·函数式编程
Forget the Dream42 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek1 小时前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学