gpt4 book ai didi

java - Tomcat 7 : WebSocket upgrade doesn't work

转载 作者:行者123 更新时间:2023-11-28 22:53:01 25 4
gpt4 key购买 nike

我正在尝试在我的 Raspberry PI 上运行我的 WebSockets 应用程序。我已经下载并解压了 Tomcat 7.0.67。然后我启动了 Tomcat-Manager 并部署了我的“wsock.jar”,它只包含一个文件:

// ChatServer.java
package wsock;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/chat")
public class ChatServer {
@OnOpen
public void onOpen(Session session) {
System.err.println("Opened session: " + session.getId());
}

@OnClose
public void onClose(Session session) {
System.err.println("Closed session: " + session.getId());
}

@OnMessage
public String onMessage(String message) {
System.err.println("Message received: " + message);
return "{ \"message\": \"Hello World\" }";
}
}

在我的本地 Tomcat 7(目前在 Windows 10)上部署时它有效:

ws = new WebSocket('ws://localhost:8080/wsock/chat');
WebSocket { url: "ws://localhost:8080/wsock/chat", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, onclose: null, extensions: "", protocol: "", onmessage: null, binaryType: "blob" }

在我的 Raspberry PI 上部署时:

ws = new WebSocket('ws://raspberrypi:8080/wsock/chat');
WebSocket { url: "ws://raspberrypi:8080/wsock/chat", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, onclose: null, extensions: "", protocol: "", onmessage: null, binaryType: "blob" }
Firefox can't establish a connection to the server at ws://raspberrypi:8080/wsock/chat.

发送的请求是:

Host: raspberrypi:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Sec-WebSocket-Version: 13
Origin: http://raspberrypi:8080
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: 0a80/+qiZ3mJ03bDgSV5kg==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

但答案不包含升级:

Content-Language: en
Content-Length: 971
Content-Type: text/html;charset=utf-8
Date: Fri, 01 Jan 2016 11:42:35 GMT
Server: Apache-Coyote/1.1

有趣的是,当我连接到示例 WebSocket Chat(随 Tomcat 服务器一起提供)时,它起作用了:

ws = new WebSocket('ws://raspberrypi:8080/examples/websocket/chat');
WebSocket { url: "ws://raspberrypi:8080/examples/webs…", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, onclose: null, extensions: "", protocol: "", onmessage: null, binaryType: "blob" }

那么,这里出了什么问题?我错过了什么?我该怎么做才能解决这个问题?

最佳答案

我在阅读配置文件并在不同机器上测试数小时后找到了答案。这是Java版本。我的 Raspberry PI 运行基于 Debian 的 Raspbian Linux。在我的 Debian 和 Raspberry 机器上它都不起作用。在我的 Windows 和 Ubuntu 机器上,它们都运行了 Java 8。 Debian 和 Raspbian 有 Java 7。我的项目是用 Java 8 支持构建的,这导致了这个问题。

我在我的 Eclipse 项目中更改了 Java 版本,现在它可以工作了。

关于java - Tomcat 7 : WebSocket upgrade doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com