Gottox socket.io-java-client“握手时出错”空指针异常
Posted
技术标签:
【中文标题】Gottox socket.io-java-client“握手时出错”空指针异常【英文标题】:Gottox socket.io-java-client "Error while handshaking" null pointer exception 【发布时间】:2013-04-26 02:38:09 【问题描述】:我正在尝试使用 socket.io 连接到 Geoloqi 托管的流媒体服务器
我直接从 github 获取了 Gottox socket.io-java-client 代码并且没有进行任何修改,除了更改 url,但它给了我“握手时出错”消息。我从 Geoloqi 的制造商那里得到的网址应该可以正常工作:https://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
代码出了什么问题?
【问题讨论】:
【参考方案1】:查看异常来自的source code(IOConnection.java:302
,来自内部NullPointerException
),有这段代码:
if (connection instanceof HttpsURLConnection)
((HttpsURLConnection) connection)
.setSSLSocketFactory(sslContext.getSocketFactory());
显然connection
必须是非空的,否则它不会通过instanceof
测试。因此,sslContext
必须为空。由于该文件中唯一引用了sslContext
的其他位置是setSslContext()
和getSslContext()
,因此唯一合乎逻辑的结论是您必须在建立SSL 连接之前调用setSslContext()
。 SocketIO.setDefaultSSLSocketFactory()
也调用到IOConnection.setSslContext()
,所以你也可以调用它。
试试这个:
SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault());
socket = new SocketIO();
socket.connect("https://subscribe.geoloqi.com:443", this);
...
【讨论】:
@Adam Rosenfield 嗨先生,我有同样的问题,我找不到任何人来帮助我,请查看此链接***.com/questions/28763858/… 谢谢【参考方案2】:当我使用 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 解决。
【讨论】:
以上是关于Gottox socket.io-java-client“握手时出错”空指针异常的主要内容,如果未能解决你的问题,请参考以下文章
Gottox socket.io-java-client“握手时出错”空指针异常
Gottox socket.io-java-client“握手时出错原因:java.io.FileNotFoundException:http://192.168.1.69:8080/socket.i
Gottox socket.io-java-client“握手时出错引起:java.io.FileNotFoundException:http://192.168.1.69:8080 / socket