Spring websocket无法进入handleMessage方法


如题,通过调试可以看到进入了handspake的interceptor和handle类的afterConnectionEstablished方法,但无法进入handleMessage方法造成无法转发消息,求高人指点。附上代码。


 package com.angularjs.utils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class TestHandler extends TextWebSocketHandler {
private static Map<String,WebSocketSession> sessionList = new HashMap<String,WebSocketSession>();


 @Override
public void afterConnectionEstablished(WebSocketSession session)
        throws Exception {
    // TODO Auto-generated method stub
    super.afterConnectionEstablished(session);
    sessionList.put(session.getAttributes().get("username").toString(), session);
}

@Override
protected void handleTextMessage(WebSocketSession session,
        TextMessage message) throws Exception {
    // TODO Auto-generated method stub
    super.handleTextMessage(session, message);
    System.out.println("begin send msg");
    WebSocketSession usersession = sessionList.get(session.getAttributes().get("username").toString());
    if(usersession!=null && usersession.isOpen()){
        usersession.sendMessage(message);
    }
}

@Override
public void handleTransportError(WebSocketSession session,
        Throwable exception) throws Exception {
    // TODO Auto-generated method stub
    super.handleTransportError(session, exception);
    if (session.isOpen()){
        sessionList.remove(session.getAttributes().get("username").toString());
    }
}

@Override
public void afterConnectionClosed(WebSocketSession session,
        CloseStatus status) throws Exception {
    super.afterConnectionClosed(session, status);
    if (session.isOpen()){
        sessionList.remove(session.getAttributes().get("username").toString());
    }
}

}

websocket java webapp java-ee spring

悲剧的同步 9 years, 1 month ago

去Log 下看一看是否出现java.lang.NoSuchMethodError找不到方法 等类似错误,原因是
有时候出现这种怪异的问题,是由于多个版本的class存在。
比如说:某个java编译成class后,放到classes下面,然后lib目录下,也有这个class所在的jar包,这样就导致classpath实际上有两个相同的class。

我就是spring.web 有两个版本而无法进入handleMessage方法,删除其中一个jar 就好了

TYPE90 answered 9 years, 1 month ago

Your Answer