I have an issue while sending the custom header in socket.io-client. Everything is okay at server side integration/code because iOS code is working fine. Here is code which I have in iOS:
let manager = SocketManager(socketURL: URL(string: "http://server_url_here/")!,
config: [.log(true),
.compress, .extraHeaders(["Authorization": "Bearer \(Defaults[.authToken])"])])
Now I tried the approach mentioned by socket.io-client documentation.Here is the code which I used:
final OkHttpClient httpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new CustomHeaderInterceptor())
.build();
//options
final IO.Options options = new IO.Options();
options.webSocketFactory = httpClient;
options.callFactory = httpClient;
//manager
Manager manager = new Manager(URI.create(Constants.BASEURL));
socket = manager.socket("/event/0", options);
socket.connect();
But this is giving the error in socket error handler:
io.socket.engineio.client.engineioexception xhr post error
I tried with this below code as well but not working:
socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport) args[0];
// Adding headers when EVENT_REQUEST_HEADERS is called
transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
Map<String, List<String>> mHeaders = (Map<String, List<String>>)args[0];
mHeaders.put("Authorization", Arrays.asList(auth_token_with_bearer));
}
});
transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
// access response headers
String auth = headers.get("Authorization").get(0);
}
});
}
});
I read many threads related to this issue but none of these are working for me. Please let me know where I'm wrong at the moment.