WebSocketClient初始化时出现NullPointer异常

时间:2017-12-14 13:20:36

标签: java jetty java-websocket

我在连接到安全的websocket时遇到了nullpointer异常。

PFB例外:

Exception in thread "main" java.lang.NullPointerException
    at org.eclipse.jetty.websocket.common.extensions.AbstractExtension.getName(AbstractExtension.java:90)
    at org.eclipse.jetty.websocket.api.extensions.ExtensionFactory.<init>(ExtensionFactory.java:37)
    at org.eclipse.jetty.websocket.common.extensions.WebSocketExtensionFactory.<init>(WebSocketExtensionFactory.java:40)
    at org.eclipse.jetty.websocket.client.WebSocketClient.<init>(WebSocketClient.java:90)
    at com.service.SecureClientSocket.main(SecureClientSocket.java:26)

PFB我的代码:

package com.service;    
import java.net.URI;
import java.util.concurrent.Future;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient;

@WebSocket
public class SecureClientSocket {
    public static void main(String []args){
        String url = "wss://qa.sockets.stackexchange.com/";
            SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setTrustAll(true); // The magic

            WebSocketClient client = new WebSocketClient(sslContextFactory);
            try
            {
                client.start();
                SecureClientSocket socket = new SecureClientSocket();
                Future<Session> fut = client.connect(socket,URI.create(url));
                Session session = fut.get();
                session.getRemote().sendString("Hello");
                session.getRemote().sendString("155-questions-active");
            }
            catch (Throwable t)
            {
               System.out.println(t.getMessage());
            }
    }
     @OnWebSocketConnect
        public void onConnect(Session sess)
        {
            System.out.println("onConnect({})"+sess);
        }

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

        @OnWebSocketError
        public void onError(Throwable cause)
        {
            System.out.println(cause);
        }

        @OnWebSocketMessage
        public void onMessage(String msg)
        {
            System.out.println("onMessage() - {}"+ msg);
        }


}

0 个答案:

没有答案