Skip to content

Socket.io Android Client not working but iOS Client Working #327

@skparticles

Description

@skparticles

http://stackoverflow.com/questions/38245464/socket-io-android-client-not-working-but-ios-client-working

We setup socket.io in our node.js server. We try to use Socket.io Java Client and Socket.io iOS Client. We able to connect through iOS but we cannot able to connect through Android. We don't have any clue , why Android is not connecting.

Server Side:

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 Not Working

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 Working

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];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions