代码之家  ›  专栏  ›  技术社区  ›  user5444681

Tomcat 7:WebSocket升级不起作用

  •  0
  • user5444681  · 技术社区  · 9 年前

    我正在尝试在复盆子PI上运行WebSockets应用程序。我已经下载并解压缩了Tomcat 7.0.67。然后我启动了TomcatManager并部署了我的“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" }
    

    在我的树莓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" }
    

    那么,这里怎么了?我错过了什么?我能做些什么来解决这个问题?

    1 回复  |  直到 9 年前
        1
  •  1
  •   user5444681 user5444681    9 年前

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

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