【发布时间】:2018-10-29 07:30:11
【问题描述】:
我正在使用带有 java 1.6 和 HTML 的 webSocket。
这是我的参考示例
https://www.byteslounge.com/tutorials/java-ee-html5-websocket-example
当我去http://localhost:8012/sessionLink/byteslounge/page.html时
错误404如下图所示
WebSocket 连接到“ws://localhost:8012/sessionLink/websocket”失败:WebSocket 握手期间出错:意外响应代码:404
这是我的代码
服务器端
@ServerEndpoint("/websocket")
public class WebSocketTest {
...
}
HTML
<script type="text/javascript">
var webSocket =
new WebSocket('ws://localhost:8012/sessionLink/websocket');
...
</script>
我是不是把 WebSocket 设置错了?
localhost:tomcat/@ServerEndpoint 中设置的端口/路径
【问题讨论】:
-
如果我尝试 new WebSocket('ws://localhost:8012/sessionLink/byteslounge') 错误将是 302
-
websocket 握手以
GET请求开始。您可以先尝试在浏览器中打开ws://localhost:8012/sessionLink/websocketURL 或确保该URL 可访问。如果不是,那么您的 URL 不正确。由于/websocket与@ServerEndpoint值匹配,因此错误可能在sessionLink中。本教程使用 GlassFish 和 Tyrus 实现。根据this,您需要 Tomcat 7+,这需要 Java 7