我們在我們的node.js服務器中設置了socket.io。我們嘗試使用Socket.io Java客戶端和Socket.io iOS客戶端。我們可以通過iOS進行連接,但無法通過Android進行連接。我們沒有任何線索,爲什麼Android沒有連接。Socket.io Android客戶端無法運行,但iOS客戶端正在運行
服務器端:
var socketIO = require('socket.io');
//var io =
GLOBAL._io = socketIO(httpServer);
_io.on('connection', (socket) => {
console.log('Client connected');
setInterval(() => _io.emit('time', new Date().toTimeString()), 1000);
socket.on('disconnect',() => console.log('Client disconnected'));
});
的Android不工作
public void addSocketConnection() throws URISyntaxException {
Log.i("Socket.io","Init");
mSocket = IO.socket("http://dev.myapp.com/parse");
mSocket.on(Socket.EVENT_CONNECT,onConnect);
mSocket.on("time",onTime);
mSocket.on(Socket.EVENT_DISCONNECT,onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.connect();
Log.i("Socket.io","Connect");
}
private Emitter.Listener onTime = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i("Socket.io","OnTime");
}
};
private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i("Socket.io","OnConnect");
}
};
private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i("Socket.io","DisConnect");
}
};
的iOS工作
NSURL* url = [[NSURL alloc] initWithString:@"http://dev.myapp.com/parse"];
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url options:@{@"log": @YES, @"forcePolling": @YES}];
[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(@"socket connected");
}];
[socket onAny:^(SocketAnyEvent *event) {
NSLog(@"On any event :%@",event.event);
}];
[socket on:@"time" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(@"Time %@",data[0]);
}];
[socket connect];
你試圖設置 <使用許可權的android:name = 「android.permission.INTERNET對」/> 到AndroidManifest.xml中文件? – mryuso92
OP找到了一個解決方案並將其發佈在[this github issue](https://github.com/socketio/socket.io-client-java/issues/327#issuecomment-232854670)中。 – Keith
是的,我忘了在StackOverflow更新。剛剛添加了答案。 – partikles