Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gottox socket.io-java-client "Error while handshaking" null pointer exception

I'm trying to use socket.io to connect to a streaming server hosted by Geoloqi

I grabbed the Gottox socket.io-java-client code straight from github and didn't make any modifications, except to change the url, but it's giving me the "Error while handshaking" message. The url should work as I got it from the makers of Geoloqi: https://community.geoloqi.com/discussion/19/data-streaming#Item_11 (see the 1st response).

Here is the code, from BasicExample.java

package basic;
/*
 * socket.io-java-client Test.java
 *
 * Copyright (c) 2012, Enno Boland
 * socket.io-java-client is a implementation of the socket.io protocol in Java.
 * 
 * See LICENSE file for more information
 */
import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import org.json.JSONException;
import org.json.JSONObject;

public class BasicExample implements IOCallback {
    private SocketIO socket;

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            new BasicExample();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public BasicExample() throws Exception {
        socket = new SocketIO();
//      socket.connect("http://localhost:8080/", this);
        socket.connect("https://subscribe.geoloqi.com:443", this);

        // Sends a string to the server.
        socket.send("Hello Server");

        // Sends a JSON object to the server.
        socket.send(new JSONObject().put("key", "value").put("key2",
                "another value"));

        // Emits an event to the server.
        socket.emit("event", "argument1", "argument2", 13.37);
    }

    @Override
    public void onMessage(JSONObject json, IOAcknowledge ack) {
        try {
            System.out.println("Server said:" + json.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(String data, IOAcknowledge ack) {
        System.out.println("Server said: " + data);
    }

    @Override
    public void onError(SocketIOException socketIOException) {
        System.out.println("an Error occured");
        socketIOException.printStackTrace();
    }

    @Override
    public void onDisconnect() {
        System.out.println("Connection terminated.");
    }

    @Override
    public void onConnect() {
        System.out.println("Connection established");
    }

    @Override
    public void on(String event, IOAcknowledge ack, Object... args) {
        System.out.println("Server triggered event '" + event + "'");
    }
}

Here is the error message:

an Error occured
io.socket.SocketIOException: Error while handshaking
    at io.socket.IOConnection.handshake(IOConnection.java:322)
    at io.socket.IOConnection.access$7(IOConnection.java:292)
    at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
Caused by: java.lang.NullPointerException
    at io.socket.IOConnection.handshake(IOConnection.java:302)
    ... 2 more
May 1, 2013 10:02:49 PM io.socket.IOConnection cleanup
INFO: Cleanup

What's going wrong with the code?

like image 447
qwikLup Avatar asked Jan 14 '23 12:01

qwikLup


1 Answers

Looking at the source code where the exception is coming from (IOConnection.java:302, from the inner NullPointerException), there's this block of code:

if (connection instanceof HttpsURLConnection) {
    ((HttpsURLConnection) connection)
        .setSSLSocketFactory(sslContext.getSocketFactory());
}

Clearly connection must be non-null, otherwise it wouldn't pass the instanceof test. Therefore, sslContext must be null. Since the only other places in that file that sslContext is referenced is in setSslContext() and getSslContext(), the only logical conclusion is that you must call setSslContext() prior to making an SSL connection. SocketIO.setDefaultSSLSocketFactory() also calls through to IOConnection.setSslContext(), so you can call that too instead.

Try this:

SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault());
socket = new SocketIO();
socket.connect("https://subscribe.geoloqi.com:443", this);
...
like image 176
Adam Rosenfield Avatar answered Jan 30 '23 00:01

Adam Rosenfield