我正在尝试使用SocketIO从Android客户端连接Node.js,但我在Logcat中面临SocketTimeOutException。
mycode的:
MainActivity.java
package com.example.androidtestclient;
import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
SocketIO socket = new SocketIO("http://10.42.0.32:3000/");
socket.connect(new IOCallback() {
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
// display("Connectet and " + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
// display("Connectet and " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
Log.d("error", "an error occured");
// display("not Connectet == error occured ");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
Log.e("disconnect", "Connection terminated");
// display("disConnectet ");
}
@Override
public void onConnect() {
System.out.println("Connection established");
Log.v("connect", "Connection established");
// display("Connectet and done");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out
.println("Server triggered event '" + event + "'");
Log.v("ON", "Server triggered event '" + event + "'");
// display("Connectet and " + event);
}
});
// This line is cached until the connection is establisched.
socket.send("Hello Server!");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
Node.js代码
var http = require('http');
var io = require('socket.io');
var app = http.createServer();
app.listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
例外:
12-07 15:28:53.396: W/System.err(4141): io.socket.SocketIOException: Error while handshaking
12-07 15:28:53.396: W/System.err(4141): at io.socket.IOConnection.handshake(IOConnection.java:322)
12-07 15:28:53.396: W/System.err(4141): at io.socket.IOConnection.access$7(IOConnection.java:292)
12-07 15:28:53.396: W/System.err(4141): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
12-07 15:28:53.396: W/System.err(4141): Caused by: java.net.SocketTimeoutException
12-07 15:28:53.396: W/System.err(4141): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
12-07 15:28:53.396: W/System.err(4141): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
12-07 15:28:53.396: W/System.err(4141): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
12-07 15:28:53.396: W/System.err(4141): at java.io.InputStream.read(InputStream.java:163)
12-07 15:28:53.396: W/System.err(4141): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
12-07 15:28:53.396: W/System.err(4141): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
12-07 15:28:53.396: W/System.err(4141): at libcore.io.Streams.readAsciiLine(Streams.java:201)
12-07 15:28:53.396: W/System.err(4141): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:547)
12-07 15:28:53.396: W/System.err(4141): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:787)
12-07 15:28:53.396: W/System.err(4141): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
12-07 15:28:53.396: W/System.err(4141): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
12-07 15:28:53.396: W/System.err(4141): at io.socket.IOConnection.handshake(IOConnection.java:313)
12-07 15:28:53.396: W/System.err(4141): ... 2 more
在浏览器中,我可以连接localhost:3000,但在Android Client中面临问题。我还在应用程序中提供了系统IP地址。
请给我一些解决方案,请告诉我。
答案 0 :(得分:0)
问题是您的客户端不支持socket.io v1.0.x并且您在服务器端使用socket.io v1.0。您应该降级服务器上的socket.io版本或更改您的库并使用另一个支持v1的库 如果您使用的是android studio,可以使用this,但如果您使用的是eclipse,我不知道任何支持socket.io v1的库