简体   繁体   English

Tomcat 7:WebSocket升级不起作用

[英]Tomcat 7: WebSocket upgrade doesn't work

I am trying to run my WebSockets Application on my Raspberry PI. 我正在尝试在Raspberry PI上运行WebSockets应用程序。 I've downloaded and unpacked Tomcat 7.0.67. 我已经下载并解压缩了Tomcat 7.0.67。 Then I started the Tomcat-Manager and deployed my "wsock.jar" that only contains one file: 然后,我启动了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\" }";
    }
}

When deploying on my local Tomcat 7 (currently on Windows 10) it works: 在我的本地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" }

When deploying on my Raspberry PI: 在我的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.

The request that was send is: 发送的请求是:

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

But the answer doesn't contain the upgrade: 但是答案不包含升级:

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

The interesting thing is, that when I connect to the examples WebSocket Chat (shipped with the Tomcat server) it works: 有趣的是,当我连接到示例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" }

So, what's wrong here? 那么,这怎么了? What am I missing? 我想念什么? What can I do to resolve this problem? 我该怎么做才能解决这个问题?

I found the answer after hours reading config files and testing on different machines. 几个小时后,我在其他计算机上读取配置文件并进行测试后找到了答案。 It was the Java version. 那是Java版本。 My Raspberry PI runs Raspbian Linux which is based on Debian. 我的Raspberry PI运行基于Debian的Raspbian Linux。 On both my Debian and Raspberry machine it didn't work. 在我的Debian和Raspberry机器上,它均不起作用。 On my Windows and Ubuntu machine, which both have Java 8 it worked. 在装有Java 8的Windows和Ubuntu计算机上,它都可以正常工作。 Debian and Raspbian have Java 7. My project was build with Java 8 support which lead to this problem. Debian和Raspbian具有Java7。我的项目是使用Java 8支持构建的,这导致了此问题。

I changed the Java version in my Eclipse project and now it works. 我在Eclipse项目中更改了Java版本,现在可以使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM