spring-boot基于websocket实现服务器推送消息到客户端
创始人
2025-01-17 21:04:56
0次

spring-boot基于websocket实现服务器推送消息到客户端

一、添加依赖

      org.springframework.boot     spring-boot-starter-websocket               org.springframework.boot         spring-boot-starter-tomcat         

一定要将tomcat依赖包排除掉 否则会报错 javax.websocket.server.ServerContainer not available

二、添加配置类

通过配置类 启用WebSocket的支持:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter;  /**  * @Author: Robin  * @ClassName: WebSocketConfig  * @Description: 开启WebSocket支持的配置类  * @DateTime: 2024/2/27 14:27  */ @Configuration public class WebSocketConfig {  	@Bean 	public ServerEndpointExporter serverEndpointExporter() { 		return new ServerEndpointExporter(); 	}  } 

三、创建WebSocketEndpoint处理类

import org.springframework.stereotype.Component; import org.springframework.stereotype.Service;  import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.concurrent.ConcurrentHashMap;  /**  * @Author: Robin  * @ClassName: WebSocketEndpoint  * @Description: websocket连接接口  * @DateTime: 2024/2/27 10:57  */ @ServerEndpoint("/wss/{id}") @Component @Service public class WebSocketEndpoint {  	// 用于存储所有打开的会话,键是唯一的客户端ID 	private static final ConcurrentHashMap sessions = new ConcurrentHashMap<>();  	private String id;  	@OnOpen 	public void onOpen(@PathParam("id") String id, Session session) { 		// 生成一个唯一的客户端ID 		sessions.put(id, session); 		this.id = id; 		System.out.println("连接已打开,客户端ID: " + id); 	}  	@OnMessage 	public void onMessage(String message, Session session) { 		System.out.println("收到消息: " + message); 	}  	@OnClose 	public void onClose(Session session) { 		String clientId = this.id; 		sessions.remove(clientId); 		System.out.println("连接已关闭,客户端ID: " + clientId); 	}  	@OnError 	public void onError(Session session, Throwable throwable) { 		String clientId = this.id; 		sessions.remove(clientId); 		System.out.println("发生错误,客户端ID: " + clientId); 		throwable.printStackTrace(); 	}  	// 用于向特定客户端发送消息的方法 	public static void sendMessageToClient(String id, String message) { 		Session session = sessions.get(id); 		if (session != null && session.isOpen()) { 			try { 				session.getAsyncRemote().sendText(message); 			} 			catch (Exception e) { 				System.out.println("发送消息时发生错误,客户端ID: " + id); 				e.printStackTrace(); 			} 		} 	}  } 

连接时传入的id为业务系统中用户的身份id便于确认当前用的身份,已key的形式存入sessions的map中,当需要给指定的用户推送消息时候只需要传入id便可找到对应的session

四、测试连接

websocket在线测试工具有很多、postman也可以进行测试

我自己用的是 WebSocket在线测试工具 (wstool.js.org)

websocket请求分为两种:ws和wss

ws:针对于没有ssl证书的域名,可以理解为http

wss:针对于有ssl证书的域名,可以理解为https

1、本地服务连接

​ 当调用本地服务测试时要采用ws的请求形式,wss是调不通的

​ ws://localhost:13400/wss/{id}
在这里插入图片描述

2、服务器连接

​ 服务器如果采用了nginx代理,则需要修改nginx配置文件,添加针对于wss请求的配置参数

location /wss {     proxy_pass http://127.0.0.1:13400;     proxy_http_version 1.1;     proxy_set_header Upgrade $http_upgrade;     proxy_set_header Connection "upgrade";     # 默认60s断开连接     proxy_read_timeout 60s; }  

​ 请求地址:wss://域名/wss/{id}

eader Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
# 默认60s断开连接
proxy_read_timeout 60s;
}

 ​	请求地址:wss://域名/wss/{id}  ​	 

相关内容

热门资讯

实测交流透视挂!宝玛大厅ai智... 实测交流透视挂!宝玛大厅ai智能透视挂教程,宝玛大厅破解软件经验(本来有挂辅助)1、每一步都需要思考...
总算了解辅助器!朱雀吴江三缺一... 总算了解辅助器!朱雀吴江三缺一ai智能透视辅助挂技巧,朱雀吴江三缺一好像是有挂(有辅助挂细节)朱雀吴...
今日公布透视!九五至尊大厅ai... 今日公布透视!九五至尊大厅ai智能透视插件,九五至尊大厅有没有透视挂窍门(切实有挂技术)1、在九五至...
玩家必看科普开挂!聚闲麻将ai... 玩家必看科普开挂!聚闲麻将ai智能透视辅助挂软件,聚闲麻将总是真的有挂(有辅助挂方式)聚闲麻将能透视...
据报道开挂!旅游娱乐ai智能开... 据报道开挂!旅游娱乐ai智能开挂工具,旅游娱乐辅助工具演示(一直有挂教学)1、全新机制【旅游娱乐ai...
一分钟揭秘开挂!决胜朋来麻将a... 一分钟揭秘开挂!决胜朋来麻将ai智能透视辅助挂教程,决胜朋来麻将真是真的是有挂(有辅助挂总结)1、首...
如何分辨真伪透视!红牛ai智能... 如何分辨真伪透视!红牛ai智能透视攻略,红牛是不是有挂经验(真是有挂技术)1、操作简单,无需红牛手机...
2分钟细说辅助器!江湖大庆麻将... 2分钟细说辅助器!江湖大庆麻将ai智能透视辅助挂攻略,江湖大庆麻将竟然是真的有挂(有辅助挂头条)1、...
总算清楚透视挂!老夫子厅ai智... 总算清楚透视挂!老夫子厅ai智能透视挂技巧,老夫子厅怎么开挂课程(好像发现有挂)老夫子厅破解侠是真的...
解密关于辅助!菜鸟麻将ai智能... 解密关于辅助!菜鸟麻将ai智能透视辅助挂脚本,菜鸟麻将总是存在有挂(有辅助挂存在)1、用户打开应用后...