2

I have socket server that need user-id in order to connect. Therefore, I need to set extraHeader to connection constructor. I tried the following code but there's no luck. Please help me get through, thanks.

Add extra header 'x-user-id': userId to connection constructor

private Socket mSocket;
{
    try {
        IO.Options opts = new IO.Options();
        mSocket = IO.socket(socket_url, opts);
        mSocket.io().on(Manager.EVENT_TRANSPORT, 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];
                        // modify request headers
                        headers.put("x-user-id", Arrays.asList("5B59F68B7B7811E88C3E52964BF487E4"));
                    }
                }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
                    @Override
                    public void call(Object... args) {
                    }
                });
            }
        });

    } catch (URISyntaxException e) {}
}

This is my swift code that is works.

private init(){
        self.socketManager = SocketManager(socketURL: URL(string:"\(MAIN_URL)/message")!, config: [.log(true),.extraHeaders(["x-user-id":AppManager.instance.userId])])
        self.socket = socketManager?.defaultSocket
    }
Junior Frogie
  • 313
  • 1
  • 3
  • 16

2 Answers2

2

You can use also stomp client provided by Spring framework. To compile and run the following method you should add 'spring-messaging' and 'spring-websocket' maven dependencies to your project.

public ListenableFuture<StompSession> connect() {
    List<Transport> transports = new ArrayList<>(2);
    transports.add(new WebSocketTransport(new StandardWebSocketClient()));
    transports.add(new RestTemplateXhrTransport());

    SockJsClient sockJsClient = new SockJsClient(transports);
    sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());

    WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    headers.add("x-user-id", "5B59F68B7B7811E88C3E52964BF487E4");

    return stompClient.connect(socket_url, headers, new StompSessionHandlerAdapter() {});
}
Tigran S.
  • 51
  • 5
0

The problem is fixed on server side. Here is my configuration. Add this >>>

var server = app.listen(3000, {// options can go here
  transports: ['xhr-polling']
});
io.use((socket, next) => {
  let clientId = socket.handshake.headers['x-user-id'];
});
Junior Frogie
  • 313
  • 1
  • 3
  • 16
  • Do you really have to specify a transport in the server? How do you deal with it in Client then? Cause, if the transport is set in the server but nothing is added to the client, the client fails to connect. – Buu97 Mar 08 '20 at 17:44