5

我正在尝试修改标准 GWT StockWatcher演示应用程序,以通过 websocket 接收股票报价。我使用gwt-ws作为我的 websockets 实现,并且根据 gwt-ws 主页的说明,我已经升级了 GWT 的嵌入式版本的 Jetty。

但是,当我尝试打开 websocket 连接时,出现以下 405 错误。有人有建议吗?

[WARN] 405 - GET /stockwatcher/webSocket (127.0.0.1) 1457 bytes
   Request headers
      Upgrade: websocket
      Connection: Upgrade
      Host: 127.0.0.1:8888
      Origin: http://127.0.0.1:8888
      Sec-WebSocket-Key: zlH08kgvDw3qHs+/OaQ9/w==
      Sec-WebSocket-Version: 13
      Sec-WebSocket-Extensions: x-webkit-deflate-frame
   Response headers
      Content-Type: text/html;charset=ISO-8859-1
      Cache-Control: must-revalidate,no-cache,no-store
      Content-Length: 1457

这是我从 onModuleLoad() 调用的客户端 websocket 代码:

private void setupWebsocket() {
    if (!JavaScriptWebSocket.IsSupported())
        return;

    //webSocketURL looks like "ws://127.0.0.1:8888/de_csenk_gwtws_demo/webSocket"
    String webSocketURL = GWT.getModuleBaseURL().replace("http", "ws") + "webSocket";

    new JavaScriptWebSocketFactory().createWebSocket(webSocketURL, new WebSocketCallback() {

        public void onOpen(WebSocket webSocket) {
            log("Web Socket Connection Open");
            webSocket.send("Test Message");
        }

        public void onMessage(WebSocket webSocket, String message) {
            log("Web Socket Connection Message: " + message);
        }

        public void onError(WebSocket webSocket) {
            log("Web Socket Connection Error");
        }

        public void onClose(WebSocket webSocket) {
            log("Web Socket Connection Closed");
        }
    });
}

我看到网页上记录了“Web Socket Connection Closed”。

这是我的 Servlet:

package com.google.gwt.sample.stockwatcher.StockWatcher.server;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.websocket.WebSocket;

import de.csenk.gwt.ws.server.jetty.JettyWebSocketConnection;
import de.csenk.gwt.ws.shared.Connection;
import de.csenk.gwt.ws.shared.Handler;

public class MyWebSocketServlet extends org.eclipse.jetty.websocket.WebSocketServlet {

    public static final long serialVersionUID = 1;

    protected WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
        return new JettyWebSocketConnection(new Handler() {
            public void onConnectionOpened(Connection connection) throws Throwable {
                System.out.println("Connection opened server side");
            }

            public void onConnectionClosed(Connection connection) throws Throwable {
                System.out.println("Connection closed server side");
            }

            public void onExceptionCaught(Connection connection, Throwable caught) {
                System.out.println("exception caught server side");
                caught.printStackTrace(System.out);
            }

            public void onMessageReceived(Connection connection, Object message) throws Throwable {
                System.out.println("Message Received! " + message);
            }
        });
    }
}

这是 StockWatcher.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='stockwatcher'>
  <!-- Inherit the core Web Toolkit stuff.                        -->
  <inherits name='com.google.gwt.user.User'/>

  <!-- Inherit the default GWT style sheet.  You can change       -->
  <!-- the theme of your GWT application by uncommenting          -->
  <!-- any one of the following lines.                            -->
  <!-- <inherits name='com.google.gwt.user.theme.clean.Clean'/>   -->
  <inherits name='com.google.gwt.user.theme.standard.Standard'/>
  <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
  <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->

  <!-- Other module inherits                                      -->
  <inherits name='de.csenk.gwt.ws.WebSocket'/>

  <!-- Specify the app entry point class.                         -->
  <entry-point class='com.google.gwt.sample.stockwatcher.StockWatcher.client.StockWatcher'/>

  <!-- Specify the paths for translatable code                    -->
  <source path='client'/>
  <source path='shared'/>


</module>

这是 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">

  <!-- Servlets -->
  <servlet>
   <servlet-name>webSocket</servlet-name>
   <servlet-class>com.google.gwt.sample.stockwatcher.StockWatcher.server.MyWebSocketServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>webSocket</servlet-name>
    <url-pattern>/stockwatcher/webSocket</url-pattern>
  </servlet-mapping>

  <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>StockWatcher.html</welcome-file>
  </welcome-file-list>

</web-app>
4

0 回答 0