在socket.io-client中发送自定义标头时遇到问题。服务器端集成/代码一切正常,因为 iOS 代码运行良好。这是我在 iOS 中的代码:
let manager = SocketManager(socketURL: URL(string: "http://server_url_here/")!,
config: [.log(true),
.compress, .extraHeaders(["Authorization": "Bearer \(Defaults[.authToken])"])])
现在我尝试了socket.io-client文档中提到的方法。这是我使用的代码:
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();
但这在套接字错误处理程序中给出了错误:
io.socket.engineio.client.engineioexception xhr 发布错误
我也尝试使用下面的代码但不工作:
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);
}
});
}
});
我阅读了许多与此问题相关的主题,但没有一个对我有用。请让我知道我现在哪里错了。