簡體   English   中英

Tomcat 7:WebSocket升級不起作用

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

我正在嘗試在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機器上,它均不起作用。 在裝有Java 8的Windows和Ubuntu計算機上,它都可以正常工作。 Debian和Raspbian具有Java7。我的項目是使用Java 8支持構建的,這導致了此問題。

我在Eclipse項目中更改了Java版本,現在可以使用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM