乐趣区

关于jeecg-boot:JeecgBoot关于websocket的改进方案

1. 环境形容

JeecgBoot3.0

2.websocket 权限认证

  • 在 shiroConfig.java 代码中正文掉
filterChainDefinitionMap.put("/websocket/**", "anon");// 零碎告诉和布告 
  • 配置拦截器
package org.jeecg.config.websocket;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
 
@Component
public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {
 
    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {return super.beforeHandshake(request, response, wsHandler, attributes);
    }
 
    @Override
    public void afterHandshake(ServerHttpRequest request,
                               ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {HttpServletRequest httpServletRequest = ((ServletServerHttpRequest)request).getServletRequest();
        HttpServletResponse httpServletResponse = ((ServletServerHttpResponse)response).getServletResponse();
        if(StringUtils.isNotEmpty(httpServletRequest.getHeader("sec-websocket-protocol")))
            httpServletResponse.addHeader("sec-websocket-protocol", httpServletRequest.getHeader("sec-websocket-protocol"));
        super.afterHandshake(request, response, wsHandler, ex);
    }
}
  • 批改前端的申请,在 HeaderNotice.vue 中
let token = Vue.ls.get(ACCESS_TOKEN)
var url = window._CONFIG['domianURL'].replace("https://","wss://").replace("http://","ws://")+"/websocket/"+userId +"?token="+token

3. HashMap 不是线程平安的,能够改为线程平安的 map,如下

 private static Map<String, Session> sessionPool = new ConcurrentHashMap<>();

4. 反复发送问题

  • 原有的代码为:
     /**
     * 服务器端推送音讯
     */
    public void pushMessage(String message) {
        try {webSockets.forEach(ws -> ws.session.getAsyncRemote().sendText(message));
        } catch (Exception e) {e.printStackTrace();
        }
    }
 
 
    @OnMessage
    public void onMessage(String message) {
        //todo 当初有个定时工作刷,应该去掉
        log.debug("【websocket 音讯】收到客户端音讯:" + message);
        JSONObject obj = new JSONObject();
        // 业务类型
        obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CHECK);
        // 音讯内容
        obj.put(WebsocketConst.MSG_TXT, "心跳响应");
        for (WebSocket webSocket : webSockets) {webSocket.pushMessage(message);
        }
    }
  • 可将 OnMessage 函数改为:
    @OnMessage
    public void onMessage(String message) {
        //todo 当初有个定时工作刷,应该去掉
        log.debug("【websocket 音讯】收到客户端音讯:" + message);
        JSONObject obj = new JSONObject();
        // 业务类型
        obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CHECK);
        // 音讯内容
        obj.put(WebsocketConst.MSG_TXT, "心跳响应");
        for (WebSocket webSocket : webSockets) {//webSocket.pushMessage(message);
            webSocket.session.getAsyncRemote().sendText(message);
        }
    }
退出移动版