Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Jetty9 WebSocket Client - SessionFactory.createSession causes java.lang.NullPointerException

Hi, I'm new to Jerry, and trying to implement WebSocket Client on Jetty9.

I saw an example on Jetty8.

org.eclipse.jetty.websocket Class WebSocketClient

http://archive.eclipse.org/jetty/8.0.0.v20110901/apidocs/org/eclipse/jetty/websocket/WebSocketClient.html

to create a new instance of WebSocketClient is :
//=====================================================
   WebSocketClientFactory factory = new WebSocketClientFactory();
   factory.start();

   WebSocketClient client = factory.newWebSocketClient();
   // Configure the client

   WebSocket.Connection connection = client.open(new
URI("ws://127.0.0.1:8080/"), new WebSocket.OnTextMessage()
   {
     public void onOpen(Connection connection)
     {
       // open notification
     }

     public void onClose(int closeCode, String message)
     {
       // close notification
     }

     public void onMessage(String data)
     {
       // handle incoming message
     }
   }).get(5, TimeUnit.SECONDS);

   connection.sendMessage("Hello World");
//===========================================================

However, I've never seen a document for Jetty9 for this.

So far, referring to
http://download.eclipse.org/jetty/9.1.0.v20131115/apidocs/org/eclipse/jetty/websocket/common/SessionFactory.html#createSession%28java.net.URI,%20org.eclipse.jetty.websocket.common.events.EventDriver,%20org.eclipse.jetty.websocket.common.LogicalConnection%29

//----------------------------------------------
WebSocketSession createSession(URI requestURI,
                             EventDriver websocket,
                             LogicalConnection connection)
//----------------------------------------------

 I've tried

//===========================================================
         try
            {
                WebSocketSession session = factory.createSession(uri,
eventDriver, connection);
                RemoteEndpoint ep = session.getRemote();
            }
         catch (Exception ex)
            {
                System.out.println("=ERROR= " + ex);
                //=ERROR= java.lang.NullPointerException
            }

        private EventDriver eventDriver = new EventDriver()
        {
            @Override
            public WebSocketPolicy getPolicy()
            {
                return null;
            }

            //......................................

            @Override
            public void incomingFrame(Frame frame)
            {

            }
        };

        private LogicalConnection connection = new LogicalConnection()
        {
            @Override
            public void close()
            {

            }

            //...............................


            @Override
            public void resume()
            {

            }
        };
//===========================================================

but I've encounter  java.lang.NullPointerException

How do we implement Jetty9 WebSocket Client ??

Thanks for your advise.

Ken


Back to the top