我正在尝试在我的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" }
那么,这里有什么问题?我错过了什么?我该怎么做才能解决这个问题?
答案 0 :(得分:1)
我在几小时后在不同的机器上读取配置文件和测试时找到了答案。这是Java版本。我的Raspberry PI运行基于Debian的Raspbian Linux。在我的Debian和Raspberry机器上它都不起作用。在我的Windows和Ubuntu机器上,它们都有Java 8,它可以工作。 Debian和Raspbian都有Java 7.我的项目是用Java 8支持构建的,这导致了这个问题。
我在Eclipse项目中更改了Java版本,现在它可以正常工作。