2
我想從Android客戶端使用SocketIO與Node.js連接,但我在Logcat中面臨SocketTimeOutException。如何使用SocketIO連接Android Client與Node.js服務器?
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地址。
請給我一些解決方案,請讓我知道。
SocketIO不喜歡使用它的其他服務,握手錯誤意味着它意識到你沒有使用SocketIO。如果你是那麼有趣 – MJPinfield 2014-12-07 12:26:42