运维开发网

Gottox socket.io-java-client“握手时出错”空指针异常

运维开发网 https://www.qedev.com 2020-06-05 14:01 出处:网络 作者:运维开发网整理
我正在尝试使用socket.io连接到 Geoloqi托管的流媒体服务器 我直接从github抓取了Gottox socket.io-java-client代码并且没有做任何修改,除了更改网址,但它给了我“握手时出错”的消息.网址应该是我从Geoloqi的制造商那里得到的:https://community.geoloqi.com/discussion/19/data-streaming#Item
我正在尝试使用socket.io连接到 Geoloqi托管的流媒体服务器

我直接从github抓取了Gottox socket.io-java-client代码并且没有做任何修改,除了更改网址,但它给了我“握手时出错”的消息.网址应该是我从Geoloqi的制造商那里得到的:https://community.geoloqi.com/discussion/19/data-streaming#Item_11(见第1回复).

这是来自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 + "'");
    }
}

这是错误消息:

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

代码出了什么问题?

查看异常来自的 source code(IOConnection.java:302,来自内部NullPointerException),这是一段代码:

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

显然连接必须是非null,否则它不会通过instanceof测试.因此,sslContext必须为null.由于该文件中仅引用sslContext的其他位置在setSslContext()和getSslContext()中,唯一合乎逻辑的结论是您必须在建立SSL连接之前调用setSslContext(). SocketIO.setDefaultSSLSocketFactory()也调用IOConnection.setSslContext(),所以你也可以调用它.

试试这个:

SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault());
socket = new SocketIO();
socket.connect("https://subscribe.geoloqi.com:443", this);
...
0

精彩评论

暂无评论...
验证码 换一张
取 消