4

我正在尝试使用 socket.io 连接到由托管的流媒体服务器Geoloqi托管的流媒体服务器

我直接从 github 获取了 Gottox socket.io-java-client 代码,除了更改 url 之外没有进行任何修改,但它给了我“握手时出错”消息。我从 Geoloqi 的制造商那里得到的网址应该可以工作:https ://community.geoloqi.com/discussion/19/data-streaming#Item_11 ://community.geoloqi.com/discussion/19/data-streaming#Item_11 (见第一个回复)。

这是来自 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

代码出了什么问题?

4

2 回答 2

9

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);
...
于 2013-05-02T04:32:10.533 回答
0

当我使用https://github.com/Gottox/socket.io-java-client创建我的 Java 客户端时,我遇到了同样的错误。好像我的服务器是基于 1.X 的,这只支持 1.0 ( https://github.com/Gottox/socket.io-java-client/issues/101 )。通过使用https://github.com/socketio/socket.io-client-java解决。

于 2016-11-15T15:29:16.057 回答