SSH框架,Websocket连接失败,求大神指导

zhaojinmeng 2016-03-31 07:03:29
环境:jdk8、tomcat8
使用的是Spring API for WebSocket,项目框架是SSH。
问题:websocket客户端一直连接失败,
报错信息:WebSocket connection to 'ws://192.168.1.110:8080/项目名/collectionList' failed: Error during WebSocket handshake: Unexpected response code: 404。
下面贴上前后台代码,求大神帮忙分析一下
...全文
921 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
晓风吹雾 2016-06-15
  • 打赏
  • 举报
回复
引用 9 楼 s09122289 的回复:
[quote=引用 8 楼 shnulaa 的回复:] The Error during the handshake happens, because Tomcat has its own api for websockets. Thus you might have added the JSR implementation or something similar as javax.websocket-api in your pom.xml there comes a conflict at runtime. Try to not export your Websocket-library to your webserver, thus it uses its own implementation. If you use maven, set the websocket dependency as provided: <!-- Provided Websocket API, because tomcat has its own implementation --> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency> 意思就是说容器中存在自己javax.websocket-api,如果是maven编译的话, 你需要吧上面的库打包到工程中。
貌似不行 这个jar已经在工程中了[/quote] 说错了,是去除工程的jar包 <scope>provided</scope> Try to not export your Websocket-library to your webserver, thus it uses its own implementation. 不要将Websocket-library导入到webserver
s09122289 2016-06-15
  • 打赏
  • 举报
回复
引用 8 楼 shnulaa 的回复:
The Error during the handshake happens, because Tomcat has its own api for websockets. Thus you might have added the JSR implementation or something similar as javax.websocket-api in your pom.xml there comes a conflict at runtime. Try to not export your Websocket-library to your webserver, thus it uses its own implementation. If you use maven, set the websocket dependency as provided: <!-- Provided Websocket API, because tomcat has its own implementation --> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency> 意思就是说容器中存在自己javax.websocket-api,如果是maven编译的话, 你需要吧上面的库打包到工程中。
貌似不行 这个jar已经在工程中了
晓风吹雾 2016-06-15
  • 打赏
  • 举报
回复
The Error during the handshake happens, because Tomcat has its own api for websockets. Thus you might have added the JSR implementation or something similar as javax.websocket-api in your pom.xml there comes a conflict at runtime. Try to not export your Websocket-library to your webserver, thus it uses its own implementation. If you use maven, set the websocket dependency as provided: <!-- Provided Websocket API, because tomcat has its own implementation --> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency> 意思就是说容器中存在自己javax.websocket-api,如果是maven编译的话, 你需要吧上面的库打包到工程中。
s09122289 2016-06-15
  • 打赏
  • 举报
回复
楼主解决了吗 我也遇到类似问题了 能分享下解决思路吗
jollroy 2016-04-01
  • 打赏
  • 举报
回复
引用 3 楼 zhaojinmeng 的回复:
核心处理类 public class RealSocketHandler implements WebSocketHandler { private static final ArrayList<WebSocketSession> clients = new ArrayList<WebSocketSession>(); private static Logger logger = Logger.getLogger(RealSocketHandler.class); }
这个地址/collectionList 你访问的到吗? 你直接在浏览器里访问这个地址看看
zhaojinmeng 2016-04-01
  • 打赏
  • 举报
回复
引用 4 楼 jollroy 的回复:
[quote=引用 3 楼 zhaojinmeng 的回复:] 核心处理类 public class RealSocketHandler implements WebSocketHandler { private static final ArrayList<WebSocketSession> clients = new ArrayList<WebSocketSession>(); private static Logger logger = Logger.getLogger(RealSocketHandler.class); }
这个地址/collectionList 你访问的到吗? 你直接在浏览器里访问这个地址看看[/quote] 在浏览器直接访问地址?这又不是http请求
zhaojinmeng 2016-04-01
  • 打赏
  • 举报
回复
前台代码: var websocket; var host=window.location.host; if ('WebSocket' in window) { alert("WebSocket"); websocket = new WebSocket("ws://192.168.1.110:8080/项目名/collectionList"); }else { alert("服务器不支持websocket"); return; } websocket.onopen = function (evnt) { alert("链接服务器成功!") }; websocket.onmessage = function (evnt) { alert("接收到信息:"+evnt.data); }; websocket.onerror = function (evnt) { alert("错误"); }; websocket.onclose = function (evnt) { alert("与服务器断开了链接!") }
zhaojinmeng 2016-03-31
  • 打赏
  • 举报
回复
后台WebSocketConfig: @Configuration @EnableWebMvc @EnableWebSocket public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer { /** * 注册websocket */ @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { // 注册多个websocket服务,addHandler第一个参数是websocket的具体业务处理类,第二个参数collectionList相当于endpoint registry.addHandler(RealSocketHandler(), "/collectionList").addInterceptors(new WebSocketHandshakeInterceptor()); // 可以注册多个websocket的endpoint } @Bean public WebSocketHandler RealSocketHandler() { return new RealSocketHandler(); } }
zhaojinmeng 2016-03-31
  • 打赏
  • 举报
回复
核心处理类 public class RealSocketHandler implements WebSocketHandler { private static final ArrayList<WebSocketSession> clients = new ArrayList<WebSocketSession>(); private static Logger logger = Logger.getLogger(RealSocketHandler.class); @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus arg1) throws Exception { // TODO Auto-generated method stub logger.debug("websocket connection closed"); System.out.println("afterConnectionClosed"); clients.remove(session); } //初次连接成功 @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // TODO Auto-generated method stub logger.debug("connect to the websocket succcess ... ..."); System.out.println("afterConnectionEstablished"); clients.add(session); String userName = (String) session.getAttributes().get("session_username"); if(userName!=null){ int count=5; session.sendMessage(new TextMessage(count+"")); } } //接收消息处理信息 @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> arg1) throws Exception { // TODO Auto-generated method stub System.out.println("handleMessage"); sendMessageToUsers(new TextMessage(arg1.getPayload()+"")); } @Override public void handleTransportError(WebSocketSession session, Throwable arg1) throws Exception { // TODO Auto-generated method stub System.out.println("handleTransportError"); logger.debug("websocket connection closed"); if(session.isOpen()){ session.close(); } clients.remove(session); } @Override public boolean supportsPartialMessages() { // TODO Auto-generated method stub return false; } /** * 给所有在线用户发送消息 * * @param message * */ public void sendMessageToUsers(TextMessage message) { for (WebSocketSession client : clients) { try { if (client.isOpen()) { client.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } } } /** * 给某个用户发送消息 * * @param userName * @param message */ public void sendMessageToUser(String userName, TextMessage message) { for (WebSocketSession client : clients) { if (client.getAttributes().get("websocket_username").equals(userName)) { try { if (client.isOpen()) { client.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } break; } } } }
zhaojinmeng 2016-03-31
  • 打赏
  • 举报
回复
捂手过滤器: public class WebSocketHandshakeInterceptor implements HandshakeInterceptor { @SuppressWarnings("unused") private static Logger logger = LoggerFactory.getLogger(HandshakeInterceptor.class); @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; HttpSession session = servletRequest.getServletRequest().getSession(false); String userName="systest"; if (session != null) { //使用userName区分WebSocketHandler,以便定向发送消息 //String userName = (String) session.getAttribute("session_username"); if(userName != null){ attributes.put("websocket_username",userName); } } } return true; } @Override public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { } }
内容概要:本文围绕基于三重移相控制(TPS)的双有源桥(DAB)高频隔离DC-DC变换器开展系统性研究,重点构建了其在Simulink环境下的高精度仿真模型。研究全面涵盖SPS单相移相、DPS双重重移相与TPS三重移相等多种控制策略的建模、实现与性能对比,深入分析不同模式下变换器的功率传输特性、软开关实现条件及功率回流问题,旨在提升DAB在交直流混合微电网、能量路由器、多端口柔性互联装置等场景中的转换效率与动态响应能力。通过对ZVS(零电压切换)条件的精确控制与移相角参数的优化,有效降低了开关损耗,增强了系统整体能效与运行稳定性。该仿真模型具有良好的可扩展性,适用于复杂电能转换系统的科研验证与工程开发。; 适合人群:电力电子、电气工程及其自动化等相关专业的硕士研究生、博士生、科研人员以及从事新能源变换器、柔性输配电系统设计的工程技术人员。; 使用场景及目标:①掌握双有源桥DAB变换器的基本工作原理及其在高频隔离场合的核心优势;②深入理解三重移相控制策略的设计机理、控制自由度分配及其在效率优化中的关键作用;③构建并调试可用于科研论文撰写、项目申报或实际系统验证的高保真Simulink仿真模型,支撑理论分析与实验对比。; 阅读建议:建议结合MATLAB/Simulink平台进行动手实践,重点关注主电路拓扑搭建、移相控制模块设计、驱动信号时序配置及ZVS实现条件的仿真观测,推荐通过对比SPS、DPS与TPS三种模式的稳态与动态响应曲线,深入掌握各控制策略的适用边界与优化方向。
【重要提示】本资源设置为0积分下载,若非0积分请勿轻易下载 亲爱的CSDN用户: 首先感谢你点进这个资源页面。我需要提前说明一个重要情况: 本资源原本已设置为“0积分下载”,即作者希望完全免费共享。但CSDN平台有时会根据文件的下载热度、文件大小、用户权限等因素,自动将部分资源的积分调整为非0数值(如1积分、2积分、5积分等)。这是平台系统的自动行为,而非作者本人的设定。 因此,如果你当前看到该资源的下载所需积分不是0(例如显示为1、2、3……),请谨慎决定是否下载。 如果你按照非0积分支付并下载后发现资源内容不符合预期、链接失效,或者实际上该资源本应是免费的,作者无法为此承担积分损失或退还操作。强烈建议:仅在页面显示为0积分时进行下载。 另外,本资源描述中并未直接提供具体的下载地址或外部链接,因为它本身是一个通过CSDN官方上传通道提交的文件/内容包。如果你看到描述中没有外部网盘地址,这是正常的——资源文件应通过CSDN内置的“下载”按钮获取。若因平台积分显示异常导致你支付了积分,请优先联系CSDN客服咨询积分退还政策,作者没有权限修改平台自动设定的积分值。 感谢你的理解与支持。技术分享本应开放,但受限于平台规则,特此提醒如上。祝学习进步!

81,111

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧