记一次坑爹的websocket Response code was not 101: 404的问题

问题描述

最近在优化公司的大屏展示功能部分,把原有低效的http轮询机制改为websocket,开发过程很顺利,部署到测试环境的时候遇到了一个坑。因为这方面资料不多,故记录一下

部署到测试环境后报错:

javax.websocket.DeploymentException 
cause by: org.glassfish.tyrus.websockets.HandshakeException: Response code was not 101: 404

发生这个错误的原因有很多种,其中有最显而易见的地址填错,因为报错信息 404 很容易让人困惑,还仔细滤了一遍防火墙,然后我查到下文

The message isn’t that cryptic, really. HTTP 1.1 status code 101 is something the WebSocket client rightfully expect. The server has to send that status code if he, during the handskake process, is willing to upgrade the currently used application protocol (HTTP!) into a WebSocket. But as the exception message reveals, the server sent a 404 code instead, saying that the resource our client was looking for couldn’t be found.

原文是 Making a Java EE 7 WebSocket ServerEndpoint class discoverable

把我排查问题的方向掰了回来,我意识到应该是环境的问题

[Servlet 3.0]以上才支持websocket,我本地tomcat是8.5,但是公司测试环境的tomcat很老了,我仿佛找到了希望。

查了一下,发现是7.0.93。确认过后发现是支持websocket的。

那为什么同样支持websocket的tomcat8.5却运行正常呢?

翻tomcat类库 lib目录的时候看到 websocket-api.jar 想起来之前碰到过一个类似问题,是tomcat类库和项目本身的jar冲突了造成的问题,我随即把项目本身的websocket包删掉,引入了tomcat类库的jar包

Over,成功连接。

总结下来,tomcat8.5在同样的问题下正常运行应该是tomcat8.5修复了jar会冲突的问题

原文地址:https://www.cnblogs.com/tinging/p/12213828.html