我们在我们的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];
+0
你试图设置 到AndroidManifest.xml中文件? –
+1
OP找到了一个解决方案并将其发布在[this github issue](https://github.com/socketio/socket.io-client-java/issues/327#issuecomment-232854670)中。 –
+0
是的,我忘了在StackOverflow更新。刚刚添加了答案。 –