記一次坑爹的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會沖突的問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM