SpringBoot整合WebSocket实现后端向前端主动推送消息方式
更新时间:2022年10月31日 10:47:42 作者:z.haoui
这篇文章主要介绍了SpringBoot整合WebSocket实现后端向前端主动推送消息方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
一、引入websocket依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
二、WebSocket配置
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}三、WebSocket服务
(前端连接地址ws://ip:端口/websocket,请自行替换ip、端口和接口名称)
@ServerEndpoint(value = "/websocket")
@Component
public class WebSocketServer {
private final static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session) {
this.session = session;
//加入set中
webSocketSet.add(this);
//在线数加1
addOnlineCount();
log.info("有新连接加入!当前在线人数为" + getOnlineCount());
try {
sendMessage("连接成功");
} catch (IOException e) {
log.error("websocket IO异常");
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
//从set中删除
webSocketSet.remove(this);
//在线数减1
subOnlineCount();
log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("来自客户端的消息:" + message);
//群发消息
for (WebSocketServer item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("发生错误");
error.printStackTrace();
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 群发自定义消息
*/
public static void sendInfo(String message) throws IOException {
log.info(message);
for (WebSocketServer item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
continue;
}
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}四、消息推送
后端调用WebServer的sendInfo接口(例如:WebSocketServer.sendInfo("Hello World");)实现主动向前端推送消息
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot+MyBatis实现MD5加密数据库用户密码的方法
MD5技术主要用于对用户密码加密,增加账户的安全性,他具有不可逆的特性,不会被轻易解密,这篇文章给大家介绍SpringBoot+MyBatis实现MD5加密数据库用户密码的方法,感兴趣的朋友跟随小编一起看看吧2024-03-03
Spring boot打包jar分离lib和resources方法实例
这篇文章主要介绍了Spring boot打包jar分离lib和resources方法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-05-05
Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot)
这篇文章主要介绍了Spring Boot集成Redis实现缓存机制(从零开始学Spring Boot),需要的朋友可以参考下2017-04-04
Spring boot按日切分spring boot的nohup.out日志文件的方法
过大的日志文件维护起来存在诸多问题,所以最好是能够按日或按大小切分日志文件,下面小编给大家带来了Spring boot按日切分spring boot的nohup.out日志文件的方法,一起看看吧2018-08-08


最新评论