从android套接字编程连接到node.js时出错。我使用socket.io连接到服务器但是一次又一次地发生超时,任何帮助都将不胜感激
代码
SocketIO socket = new SocketIO("http://10.10.10.125:1337");
socket.connect
(
new IOCallback(){
@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");
//socket.send("Hello Server!");
//System.out.println("send msg to server");
}
@Override
public void onMessage(String arg0,
IOAcknowledge arg1) {
// TODO Auto-generated method stub
System.out.println("Server said: " + arg0);
}
@Override
public void onMessage(JSONObject arg0,
IOAcknowledge arg1) {
// TODO Auto-generated method stub
try {
System.out.println("Server said:" + arg0.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void on(String arg0, IOAcknowledge arg1,
Object... arg2) {
// TODO Auto-generated method stub
System.out.println("on....");
}
}
);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}

错误
12-04 16:30:30.510: W/System.err(8986): io.socket.SocketIOException: Error while handshaking
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection.handshake(IOConnection.java:322)
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection.access$600(IOConnection.java:39)
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
12-04 16:30:30.510: W/System.err(8986): Caused by: java.net.SocketTimeoutException
12-04 16:30:30.510: W/System.err(8986): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
12-04 16:30:30.510: W/System.err(8986): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
12-04 16:30:30.510: W/System.err(8986): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
12-04 16:30:30.510: W/System.err(8986): at java.io.InputStream.read(InputStream.java:163)
12-04 16:30:30.510: W/System.err(8986): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
12-04 16:30:30.510: W/System.err(8986): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
12-04 16:30:30.510: W/System.err(8986): at libcore.io.Streams.readAsciiLine(Streams.java:201)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
12-04 16:30:30.510: W/System.err(8986): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
12-04 16:30:30.510: W/System.err(8986): at io.socket.IOConnection.handshake(IOConnection.java:313)
12-04 16:30:30.510: W/System.err(8986): ... 2 more

答案 0 :(得分:1)
问题是您的客户端库支持socket.io 0.9.x并且您的服务器正在使用socket.io v1并且从0.9到1,在socket.io库中更改了握手协议。尝试将服务器的socket.io降级为0.9并且它可以正常工作,或者您可以使用支持socket.io v1.0.x的socket.io-client