springCloud(若依微服务版)集成WebSocket实现消息推送
转载自CSDN:原文链接:blog.csdn.net/weixin_4393…
pom文件引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
添加WebSocket配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @Description websocket 配置
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
新建socket目录,在socket目录下添加WebSocketClient存储连接的Session和Uri
import javax.websocket.Session;
public class WebSocketClient {
/**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
private Session session;
/**
* 连接的uri
*/
private String uri;
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
}
在socket目录下添加WebSocketService服务类
@Slf4j
@ServerEndpoint(value = "/websocket/{userName}")
@Component
public class WebSocketService {
/**
* 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
*/
private static int onlineCount = 0;
/**
* concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。
*/
private static ConcurrentHashMap<String, WebSocketClient> webSocketMap = new ConcurrentHashMap<>();
/**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
private Session session;
/**
* 接收userName
*/
private String userName = "";
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session, @PathParam("userName") String userName) {
if (!webSocketMap.containsKey(userName)) {
// 在线数 +1
addOnlineCount();
}
this.session = session;
this.userName = userName;
WebSocketClient client = new WebSocketClient();
client.setSession(session);
client.setUri(session.getRequestURI().toString());
webSocketMap.put(userName, client);
log.info("----------------------------------------------------------------------------");
log.info("用户连接:" + userName + ",当前在线人数为:" + getOnlineCount());
try {
sendMessage("连接成功");
} catch (IOException e) {
log.error("用户:" + userName + ",网络异常!");
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
if (webSocketMap.containsKey(userName)) {
webSocketMap.remove(userName);
if (webSocketMap.size() > 0) {
//从set中删除
subOnlineCount();
}
}
log.info("----------------------------------------------------------------------------");
log.info(userName + "用户退出,当前在线人数为:" + getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("收到用户消息:" + userName + ",报文:" + message);
//可以群发消息
//消息保存到数据库、redis
if (StringUtils.isNotBlank(message)) {
}
}
/**
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("用户错误:" + this.userName + ",原因:" + error.getMessage());
error.printStackTrace();
}
/**
* 连接服务器成功后主动推送
*/
public void sendMessage(String message) throws IOException {
synchronized (session) {
this.session.getBasicRemote().sendText(message);
}
}
/**
* 向指定客户端发送消息
*
* @param userName
* @param message
*/
public static void sendMessage(String userName, String message) {
try {
WebSocketClient webSocketClient = webSocketMap.get(userName);
if (webSocketClient != null) {
webSocketClient.getSession().getBasicRemote().sendText(message);
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketService.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketService.onlineCount--;
}
public static void setOnlineCount(int onlineCount) {
WebSocketService.onlineCount = onlineCount;
}
public static ConcurrentHashMap<String, WebSocketClient> getWebSocketMap() {
return webSocketMap;
}
public static void setWebSocketMap(ConcurrentHashMap<String, WebSocketClient> webSocketMap) {
WebSocketService.webSocketMap = webSocketMap;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
解释
- 注意这里 WebSocketClient 是上面新建的用来存储连接相关信息的实体类
- @ServerEndpoint(value = “/websocket/{userName}”)
这里的就是websocket连接的地址,后面的{userName}是用来接收前端传递的参数,用作不同标识
WebSocketService 方法解释
- OnOpen 注解的方法是连接建立成功后调用的方法,可以根据自己的业务需要去处理。
- OnClose 注解的是连接关系调用的方法。
- OnMessage 注解的是客户端发来消息时的回调方法,里面根据自己需要去处理数据。
- sendMessage 方法在客户端连接到服务器时,使用当前会话来给客户端推送一个反馈消息,然后向指定客户端发送消息使用的是sendMessage方法,传递的是用户的唯一标识和消息内容。
websocket 访问路径放开
1. 注册到nacos的话
2. 项目内
演示
- 启动项目
- 找个在线WebSocket调试工具(www.wetools.com/websocket/)
- 输入服务地址ws://127.0.0.1:8080/plan/websocket/admin
8080为网关的端口号,/plan为服务名称,/websocket为WebSocketService类中的路径,admin为用户唯一标识
下面是在自己项目中的应用
1、ruoyi-framework/pom.xml文件添加websocket依赖。
<!-- SpringBoot Websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2、配置匿名访问(可选)
// 如果需要不登录也可以访问,需要在`SecurityConfig.java`中设置匿名访问
("/websocket/**").permitAll()
我写到了这里:
3. WebSocketConfig 固定写法
package com.dkd.framework.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* websocket 配置
*
* @author ruoyi
*/
@Configuration
public class WebSocketConfig
{
@Bean
public ServerEndpointExporter serverEndpointExporter()
{
return new ServerEndpointExporter();
}
}
5. WebSocketClient 实体类
package com.dkd.framework.websocket;
import lombok.Data;
import javax.websocket.Session;
@Data
public class WebSocketClient {
/**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
private Session session;
/**
* 连接的uri
*/
private String uri;
}
7. WebSocketService 主要用到的类
package com.dkd.framework.websocket;
import com.dkd.common.utils.StringUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
@Setter
@Getter
@Slf4j
@ServerEndpoint(value = "/websocket/{userName}")
@Component
public class WebSocketService {
/**
* 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
*/
private static int onlineCount = 0;
/**
* concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。
*/
@Getter
private static ConcurrentHashMap<String, WebSocketClient> webSocketMap = new ConcurrentHashMap<>();
/**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
private Session session;
/**
* 接收userName
*/
private String userName = "";
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session, @PathParam("userName") String userName) {
if (!webSocketMap.containsKey(userName)) {
// 在线数 +1
addOnlineCount();
}
this.session = session;
this.userName = userName;
WebSocketClient client = new WebSocketClient();
client.setSession(session);
client.setUri(session.getRequestURI().toString());
webSocketMap.put(userName, client);
log.info("----------------------------------------------------------------------------");
log.info("用户连接:" + userName + ",当前在线人数为:" + getOnlineCount());
try {
sendMessage("连接成功");
} catch (IOException e) {
log.error("用户:" + userName + ",网络异常!");
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
if (webSocketMap.containsKey(userName)) {
webSocketMap.remove(userName);
if (webSocketMap.size() > 0) {
//从set中删除
subOnlineCount();
}
}
log.info("----------------------------------------------------------------------------");
log.info(userName + "用户退出,当前在线人数为:" + getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("收到用户消息:" + userName + ",报文:" + message);
//可以群发消息
//消息保存到数据库、redis
if (StringUtils.isNotBlank(message)) {
}
}
/**
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("用户错误:" + this.userName + ",原因:" + error.getMessage());
error.printStackTrace();
}
/**
* 连接服务器成功后主动推送
*/
public void sendMessage(String message) throws IOException {
synchronized (session) {
this.session.getBasicRemote().sendText(message);
}
}
/**
* 向指定客户端发送消息
*
* @param userName
* @param message
*/
public static void sendMessage(String userName, String message) {
try {
WebSocketClient webSocketClient = webSocketMap.get(userName);
if (webSocketClient != null) {
webSocketClient.getSession().getBasicRemote().sendText(message);
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketService.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketService.onlineCount--;
}
public static void setOnlineCount(int onlineCount) {
WebSocketService.onlineCount = onlineCount;
}
public static void setWebSocketMap(ConcurrentHashMap<String, WebSocketClient> webSocketMap) {
WebSocketService.webSocketMap = webSocketMap;
}
}
9. 测试类 WebSocket
package com.dkd.manage.controller;
import com.dkd.common.annotation.Anonymous;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.framework.websocket.WebSocketService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wuJiaWei
* @version 1.0
*/
@Anonymous
@RestController
@RequestMapping("/WebSocket/demo")
public class WebSocket {
@GetMapping
public AjaxResult demo() {
WebSocketService.sendMessage("admin","hello a 树哥");
WebSocketService.sendMessage("13811259343","hello a 树哥");
return AjaxResult.success("success");
}
}
@ServerEndpoint
是 Java API for WebSocket (JSR 356) 中的一个注解,用于标记一个类作为 WebSocket 服务器端点。这个注解
使得被标记的类能够处理 WebSocket 连接请求,并且定义了 WebSocket 的行为,比如如何处理打开连接、关闭连
接、接收消息等事件。