Search code examples
javaandroidsocket.iosocket.io-1.0socket.io-java-client

Android socket.io client


I'm trying to set up a android socket.io client, but I'm having some difficulties. Right now I'm using this library:https://github.com/nkzawa/socket.io-client.java and everything is working great when we turn off the middleware. When it's turned on I need to send a header while connecting to the server but as I understand this library doesn't provide such possibility. So can someone propose a different library or share a workaround for this issue?

Thanks


Solution

  • I am using this library for a while and have found it very adequate. If you like to sent header, you may follow sample below; For version [0.6.0] (previous versions is little bit different)

    1 - Create socket.io client

            IO.Options opts = new IO.Options();
    
            socket = IO.socket("http://server_address", opts);
            socket.io().on(Manager.EVENT_TRANSPORT, onTransport);
    

    2 - Implement callback handler onTransport

    private Emitter.Listener onTransport = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
    
            Transport transport = (Transport)args[0];
            transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    @SuppressWarnings("unchecked")
                    Map<String, List<String>> headers = (Map<String, List<String>>) args[0];
                    String bearer = "bearer " + session.token;
                    headers.put("Authorization", Arrays.asList(bearer));
                }
            }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                }
            });
        }
    };        
    

    In example above, I set Authorization header but you are free to set whatever you need.