SpringBoot整合websocket踩的坑
最近做一个项目需要为前端大屏推送信息,使用的是SpringBoot,需在项目中整合websocket实时推送信息,且只用有连接的时候再推送。
第一步:在SpringBoot中整合进入websocket
先是pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
无需再配置文件中添加任何配置,可直接使用项目端口。
第二步:websocket配置类
添加websocket配置类 WebSocketConfig.java
/**
* @Component
* @since jdk1.8
*/
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
第三步:各节点监听发放类
MonitorWebSocket.java
/**
*
* 此处在本地启动和Linux服务器部署启动会有冲突,最后详细说
*
*/
@Component
@ServerEndpoint(value = "/websocket/monitor/{userId}")
public class MonitorWebSocket {
private static final Logger logger = LoggerFactory.getLogger(MonitorWebSocket.class);
/**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
private static int onlineCount = 0;
/**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
private static ConcurrentHashMap<String, MonitorWebSocket> webSocketMap = new ConcurrentHashMap<>();
/**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
private Session session;
/**接收userId*/
private String userId="";
/**
* 连接建立成功调用的方法*/
@OnOpen
public void onOpen(Session session, @PathParam("userId") String userId) {
this.session = session;
this.userId=userId;
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
webSocketMap.put(userId,this);
//加入set中
}else{
webSocketMap.put(userId,this);
//加入set中
addOnlineCount();
//在线数加1
}
logger.info("/websocket/monitor/用户连接:"+userId+",当前在线人数为:" + getOnlineCount());
try {
sendMessage(JSONObject.toJSONString(R.ok("连接成功")));
} catch (IOException e) {
logger.error("用户:{},网络异常!!!!!!:{}",userId,e.getMessage());
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
//从set中删除
subOnlineCount();
}
logger.info("用户退出:{},当前在线人数为:{}", userId, getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("用户消息:"+userId+",报文:"+message);
//可以群发消息
//消息保存到数据库、redis
if(StringUtils.isNotBlank(message)){
try {
//解析发送的报文
JSONObject jsonObject = JSONObject.parseObject(message);
//追加发送人(防止串改)
jsonObject.put("fromUserId",this.userId);
String toUserId=jsonObject.getString("toUserId");
//传送给对应toUserId用户的websocket
if(StringUtils.isNotBlank(toUserId)&&webSocketMap.containsKey(toUserId)){
webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());
}else{
logger.info("请求的userId:"+toUserId+"不在该服务器上");
//否则不在这个服务器上,发送到mysql或者redis
}
}catch (Exception e){
logger.error(e.getMessage());
}
}
}
/**
* session会话错误
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
logger.error("用户错误:{},原因:{}", this.userId, error.getMessage());
}
/**
* 实现服务器主动推送
*/
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 发送自定义消息
* */
public static void sendInfo(String message,@PathParam("userId") String userId) throws IOException {
logger.info("发送消息到:{},报文:{}", userId, message);
if(StringUtils.isNotBlank(userId)&&webSocketMap.containsKey(userId)){
webSocketMap.get(userId).sendMessage(message);
}else{
logger.error("用户{},不在线!", userId);
}
}
/**
* 给所有用户连接推送消息
* @author cd
* @date 2020/3/10
* @param message
* @return
*/
public static void sendMessageAll(String message) throws IOException {
for (MonitorWebSocket item : webSocketMap.values()) {
item.session.getAsyncRemote().sendText(message);
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
MonitorWebSocket.onlineCount++;
}
public static synchronized void subOnlineCount() {
MonitorWebSocket.onlineCount--;
}
}
第四步:前端页面写法
// 建立websocket连接,进入页面后台推送数据
var websocket = null;
var host = document.location.host;
// 获得一个随机数作为连接的唯一标识, 此处是封装的一个获取指定位数的随机数方法
var username = RndNum(10);
//判断当前浏览器是否支持WebSocket
if (websocket != null) {
websocket.close();
websocket = null;
}
if ('WebSocket' in window) {
websocket = new WebSocket('ws://' + host + '/websocket/monitor/' + username);
} else {
alert('当前浏览器 Not support websocket')
}
//连接成功建立的回调方法
websocket.onopen = function () {
console.log("websocket通道连接成功!")
}
//连接发生错误的回调方法
websocket.onerror = function () {
console.log("websocket连接发生错误!")
};
//接收到消息的回调方法
websocket.onmessage = function (event) {
setMessageInnerHTML(event.data);
}
//连接关闭的回调方法
websocket.onclose = function () {
console.log("websocket连接关闭!")
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function () {
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(msg) {
// document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
以下是踩的坑
按照上面的写法程序在本地启动、测试,不会有任何问题,但是当把项目部署到Linux服务器的tomcat 中时启动会报错
java.lang.IllegalStateException: Failed to register @ServerEndpoint class:
为了解决这个问题本人在网上查找了大量的资料,基本的解决办法就是说
MonitorWebSocket类中不需要在@ServerEndpoint之前使用@Component注解,去掉后就可以在服务器正常启动
还有一种是说添加jar包的,这种本人也试过,服务器确实可以正常启动
以上两种解决方法都可以解决服务器启动报错问题,但是这种写法在本地启动项目的时候前端无法建立连接,还是不能推送数据,至今未找到如何使本地和服务器同时满足不需要修改代码的解决方案,最后只能在部署到服务器时将MonitorWebSocket.java类中的@Component注解去掉,若哪位大神有更好的解决办法留言私信我,谢谢
#修改方法(已测试)
将项目中引入的webSocket的jar包修改为
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>