Closed ghost closed 3 years ago
Do you find way to do it yet? I'm stuck at this too.
Use OkHttpClient with Interceptor, and add your custom headers there, like this:
final OkHttpClient httpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new CustomHeaderInterceptor())
.build();
Then create socket like this:
final URI socketUri = // ...
final IO.Options options = new IO.Options();
options.webSocketFactory = httpClient;
options.callFactory = httpClient;
final Socket socket = IO.socket(socketUri, options);
@KonstantinBerkow Thanks. After five hours of work, the problem was solved. :) 👍
According to the README, you can add a custom header with:
socket.on(Socket.EVENT_TRANSPORT, new Emitter.listener() {
@Override
public void call(Object... args) {
// Called on a new transport created.
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];
headers.put("Authorization", Arrays.asList("Bearer xxxx"));
}
});
}
});
But based on the number of questions around this, I think it might make sense to add a headers
map in the Socket.Options
object.
Convert this code in JAVA
iOS Swift code
let manager = SocketManager(socketURL: URL(string: "http://2de0c832.ngrok.io/")!, config: [.log(true), .compress, .extraHeaders(["Authorization": "Bearer \(Defaults[.authToken])"])])