1

桟橋の websocket の例を機能させるのに問題があります

public class App {

    public static void main(String[] args)  {

        Server server = new Server(8080);

        ServletHandler handler = new ServletHandler();
        server.setHandler(handler);

        handler.addServletWithMapping(MyEchoServlet.class, "/test/*");

        try {
            server.start();
            server.join();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public class MyEchoServlet extends WebSocketServlet {

        @Override
        public void configure(WebSocketServletFactory factory) {
            //factory.getPolicy().setIdleTimeout(10000);
            factory.register(MyEchoSocket.class);
        }
    }
     @WebSocket(maxMessageSize = 64 * 1024)
    public class MyEchoSocket {

        Session session;

        @OnWebSocketConnect
        public void onConnect(Session session){
            this.session = session;
            System.out.println("init "+session.getUpgradeRequest().getRequestURI().getPath() );
        }

        @OnWebSocketMessage
        public void onText(String msg) {
             System.out.println("rec "+msg );
            try {
                session.getRemote().sendString(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @OnWebSocketClose
        public void onClose(int statusCode, String reason) {
            System.out.println("MyWebSocket.onClose()");
        } 
    }
}

JavaScript から Websocket を作成しようとするとすぐに、次のエラーが発生します。

var ws = new WebSocket("ws://localhost:8080/test/");

サーバーエラー:

2013-09-14 15:57:04.076 INFO  Server - jetty-9.0.5.v20130815
2013-09-14 15:57:04.122 INFO  ServerConnector - Started ServerConnector@1a51ce0{HTTP/1.1}{0.0.0.0:8080}
2013-09-14 15:57:11.700 WARN  ContextHandler - unavailable
java.lang.InstantiationException: app.App$MyEchoServlet
    at java.lang.Class.newInstance(Unknown Source)
    at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:979)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:523)
    at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:424)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:671)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:434)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:445)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:268)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
    at java.lang.Thread.run(Unknown Source)
2013-09-14 15:57:11.706 WARN  ServletHandler - 
javax.servlet.ServletException: app.App$MyEchoServlet-4598284@e19c6ca5==app.App$MyEchoServlet,-1,false
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:563)
    at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:424)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:671)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:434)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:445)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:268)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.InstantiationException: app.App$MyEchoServlet
    at java.lang.Class.newInstance(Unknown Source)
    at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:979)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:523)
    ... 13 more

jetty バージョン: 9.0.5v20130815 (maven eclipse.jetty レポから)

4

1 に答える 1

4

サーブレットとソケットの両方を内部クラスとして宣言したので、コードの次の 2 行を変更します ...

// From
public class MyEchoServlet extends WebSocketServlet {
// To
public static class MyEchoServlet extends WebSocketServlet {


// From
public class MyEchoSocket {
// To
public static class MyEchoSocket {

これは、Java が非静的内部クラスを単にインスタンス化できないためです。

main() メソッドからこれを試してみると...

Servlet test = new MyEchoServlet();
于 2013-09-14T14:13:05.960 に答える