0

I have two socketio servers behind haproxy, and for the purpose of stickiness i am inserting the cookie at haproxy, to identify the server from which response came from. Now the problem is, when i do so using the code below, my connection is not getting upgraded to 'websocket', i tried to debug the issue and realized that 'transport' event is only emitted in 'setTransport' method in engineio socket class, and not in the 'createTransport' method, (in short during probe 'transport' event is not emitted, leading to websocket handshake request going on different socketio server than where the client is already connected using xhrpolling).

I was wondering if there is some other work around this issue, or the problem is genuine. Plz help out. Thanks in advance.

link to - issue with code that i have tried

final StringBuffer buf = new StringBuffer();
socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        final Transport transport = (Transport)args[0];

        if(transport.name.equalsIgnoreCase(WebSocket.NAME)){
            transport.once(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    @SuppressWarnings("unchecked")
                    Map<String, String> headers = (Map<String, String>)args[0];
                    if(buf.length()!=0){
                        headers.put("Cookie", buf.toString());
                    }
                }
            });
        }
        if(transport.name.equalsIgnoreCase(PollingXHR.NAME)){
            transport.once(Transport.EVENT_RESPONSE_HEADERS,
                    new Emitter.Listener() {
                        @Override
                        public void call(Object... args) {
                            @SuppressWarnings("unchecked")
                            Map<String, String> headers = (Map<String, String>) args[0];
                            String cookie = null;
                            if (headers.containsKey("Set-Cookie") && headers.get("Set-Cookie").contains("SERVERID")) {
                                cookie = headers.get("Set-Cookie").split(";")[0];
                            }
                            if (cookie != null) {
                                final String stickyCookie = cookie;
                                if(buf.length()==0){
                                    buf.append(stickyCookie);
                                }
                                transport.on(
                                        Transport.EVENT_REQUEST_HEADERS,
                                        new Emitter.Listener() {
                                            @Override
                                            public void call(Object... args) {
                                                @SuppressWarnings("unchecked")
                                                Map<String, String> headers = (Map<String, String>) args[0];
                                                // set header                                               
                                                headers.put("Cookie", stickyCookie);
                                            }
                                        });
                            }
                        }
                    });
        }            
    }
});
vishal
  • 29
  • 2

1 Answers1

0

i found workaround my problem for now as haproxy provides another balancing algorithm 'source' which direct requests coming from the same 'ip' to the same server ... as it hashes on the source of the request. And that way i don't have to use all this code above .. (ie. i don't have to do anything on transport event). haproxy takes care of everything... :)

vishal
  • 29
  • 2