我想要的只是从我的Android应用程序连接到我的nodejs服务器,顺便说一句,我使用genymotion。
这里有什么问题:
节点服务器:
var server = require('http').Server();
var io = require('socket.io')(server);
io.sockets.on('connection', function(socket) {
socket.on('echo', function(data) {
socket.emit('echo back', data);
});
});
server.listen(12123,"127.0.0.1");
Android:
SocketIO socket = new SocketIO("http://127.0.0.1:12123");
socket.connect(new IOCallback() {
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
if ("echo back".equals(event) && args.length > 0) {
Log.d("SocketIO", "" + args[0]);
// -> "hello"
}
}
@Override
public void onDisconnect() {
}
@Override
public void onConnect() {
Log.i("CONNECT","just connected");
}
@Override
public void onMessage(String s, IOAcknowledge ioAcknowledge) {
Log.i("MESSAGE",s);
}
@Override
public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) {
}
@Override
public void onError(SocketIOException e) {
Log.i("ERROR_MESSAGE ",e.getMessage().toString());
}
});
socket.emit("echo", "hello");
Logcat:
01-26 17:04:44.996 1904-2291/com.navigation.drawer.activity I/ERROR_MESSAGE﹕ Error while handshaking
01-26 17:04:44.997 1904-2291/com.navigation.drawer.activity I/io.socket﹕ Cleanup
节点版本v0.10.31 socket.io版本1.3.2
答案 0 :(得分:0)
您无法从模拟器访问127.0.0.1(localhost),因为模拟器有自己的本地主机,我认为12.0.0.2(或其他),所以我认为如果您直接连接到本地IP(例如:192.168) .1.123,你的本地IP是什么),它必须工作