java - Spring websocket无法进入handleMessage方法
迷茫
迷茫 2017-04-17 13:22:43
[Java讨论组]

如题,通过调试可以看到进入了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());
    }
}

}

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
巴扎黑

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号