【websocket】sockjs握手请求发送成功,但是没有握手成功

Richard_Yi
  • 2.1k

背景

使用spring websocket实现后台给前端推送消息

在支持websocket的浏览器版本上能够正常实现消息的收发,现在使用sockjs做为不支持websocket的浏览器的兼容方案。

问题

浏览器支持websocket时:

clipboard.png

并且后端会输出对应的连接成功的日志信息

clipboard.png

当浏览器不支持websocket时,使用sockjs:

发现握手请求发送成功了,但是并没有握手成功,连接没有建立起来,也没有相关的日志信息输出。

clipboard.png

clipboard.png

"/info"和"t="是sockjs自己加上去的,我原来的url是http://localhost:8081/FGMSP-ui/sockjs/websocket?uid=2131231

问题一

为什么sockjs会在我原有的url上自动加上info的后缀和t这个参数?

另外sockjs之后还发起过很多次类似的请求,如下图

clipboard.png

都是在我原先的url上加了后缀和参数,这是什么原理啊?

问题二

为什么我的第一次的握手请求发送显示成功了(在调试DispatcherServlet类的doDispatch()时也看到了这个请求已经进来),但是没有被interceptor和handler处理?是我的配置出错了吗?

下面是我的一些代码和配置

代码

config

@Configuration
@EnableWebMvc
@EnableWebSocket
public class MySocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {

    @Autowired
    private MySocketHandler mySocketHandler;

    @Autowired
    private MyInterceptor myInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(mySocketHandler, "/websocket")
                .addInterceptors(myInterceptor);
        // 浏览器不支持websocket,使用sockjs建立连接
        registry.addHandler(new MySocketHandler(), "/sockjs/websocket/info")
                .addInterceptors(myInterceptor)
                .withSockJS();
    }

}

interceptor

public class MyInterceptor implements HandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
                                   WebSocketHandler webSocketHandler, Map<String, Object> attributes) throws Exception {
//        System.out.println("Websocket:用户[ID:" + ((ServletServerHttpRequest) request).getServletRequest().getSession(false).getAttribute("uid") + "]已经建立连接");
        if (request instanceof ServletServerHttpRequest) {
            // 获取请求参数,首先我们要获取HttpServletRequest对象才能获取请求参数;当ServerHttpRequset的层次结构打开后其子类可以获取到我们想要的http对象,那么就简单了。
            // 我这里是把获取的请求数据绑定到session的map对象中(attributes)
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String id = servletRequest.getSession().getId();
            // 标记用户
            String uid = servletRequest.getParameter("uid");
            attributes.put("uid", uid);
            if(uid!=null){
                attributes.put("uid", uid);
                System.out.println(String.format("websocket建立握手前:sessionId[%s],userId[%s]", id, uid));
            }else{
                return false;
            }
        }
        return true;
    }


    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {
        System.out.println("握手后");
    }
}

handler

public class MySocketHandler implements WebSocketHandler {

    private static final Logger logger = Logger.getLogger(MySocketHandler.class);

    // websocket连接建立
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        logger.info("Websocket Connection established");
        logger.info("getId:" + session.getId());
        logger.info ("getLocalAddress:" + session.getLocalAddress().toString());
        logger.info ("getUri:" + session.getUri().toString());
        session.sendMessage(new TextMessage("Server:connected OK!"));
        String uid = (String) session.getHandshakeAttributes().get("uid");
        if (SocketSessionMap.socketSession.get(uid) == null) {
            SocketSessionMap.socketSession.put(uid, session);
        }
    }

    // 消息处理
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {

    }

    // 消息传输错误
    @Override
    public void handleTransportError(WebSocketSession session, Throwable throwable) throws Exception {
        if (session.isOpen()) {
            session.close();
        }
        // 移除Socket会话
        Iterator<Map.Entry<String, WebSocketSession>> iter = SocketSessionMap.socketSession.entrySet().iterator();
        while (iter.hasNext()) {
            ConcurrentHashMap.Entry entry = iter.next();
            if (entry.getValue() == session) {
                SocketSessionMap.socketSession.remove(entry.getKey());
                System.out.println("Socket会话已经移除:用户ID" + entry.getKey());
                break;
            }
        }
    }

    // websocket 连接关闭
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Websocket:" + session.getId() + "已经关闭");
        // 移除Socket会话
        Iterator<Map.Entry<String, WebSocketSession>> iter = SocketSessionMap.socketSession.entrySet().iterator();
        while (iter.hasNext()) {
            ConcurrentHashMap.Entry entry = iter.next();
            if (entry.getValue() == session) {
                SocketSessionMap.socketSession.remove(entry.getKey());
                System.out.println("Socket会话已经移除:用户ID" + entry.getKey());
                break;
            }
        }

    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }

    /**
     * 给某个用户发送消息
     *
     * @param uid
     * @param message
     * @throws IOException
     */
    public void sendMessageToUser(String uid, TextMessage message)
            throws IOException {
        WebSocketSession session = SocketSessionMap.socketSession.get(uid);
        if (session != null && session.isOpen()) {
            session.sendMessage(message);
        }
    }

}

配置

DispatcherServlet配置

<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    ...
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/websocket</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/sockjs/websocket/info</url-pattern>
    </servlet-mapping>
<servlet>
    <bean id="mySocketHandler" class="com.fingard.rh.msp.socket.MySocketHandler"/>

    <!-- 握手接口/拦截器 -->
    <bean id="myInterceptor" class="com.fingard.rh.msp.socket.MyInterceptor"/>

    <websocket:handlers >
        <websocket:mapping path="/websocket" handler="mySocketHandler"/>
        <websocket:handshake-interceptors>
            <ref bean="myInterceptor"/>
        </websocket:handshake-interceptors>
    </websocket:handlers>

    <!-- sockJS -->
    <websocket:handlers>
        <websocket:mapping path="/sockjs/websocket/info" handler="mySocketHandler"/>
        <websocket:handshake-interceptors>
            <ref bean="myInterceptor"/>
        </websocket:handshake-interceptors>
        <websocket:sockjs />
    </websocket:handlers>

问题一、问题二。请不吝赐教,万分感激!!

回复
阅读 3.7k
1 个回答
坏坏de想你
  • 2
新手上路,请多包涵

你好,楼主这个问题解决了么?请问怎么解决的呢?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏