Java okhttp3 websocket动态标题
我试图创建一个websocket,并在发送的每封邮件中动态地重新计算其标题。可能吗 我试图使用拦截器,但只被调用了一次Java okhttp3 websocket动态标题,java,android,websocket,okhttp3,Java,Android,Websocket,Okhttp3,我试图创建一个websocket,并在发送的每封邮件中动态地重新计算其标题。可能吗 我试图使用拦截器,但只被调用了一次 public void run() { // only open a websocket if there aren't websockets already open if (this.webSocket == null || !this.openingWS) { this.openingWS = true; wsBuil
public void run() {
// only open a websocket if there aren't websockets already open
if (this.webSocket == null || !this.openingWS) {
this.openingWS = true;
wsBuilder = new OkHttpClient.Builder();
OkHttpClient client = wsBuilder.addInterceptor(this)
.readTimeout(0, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder()
.url("wss://...")
.build();
client.newWebSocket(request, this);
// Trigger shutdown of the dispatcher's executor so this process can exit cleanly.
client.dispatcher().executorService().shutdown();
}
}
@Override public void onOpen(WebSocket webSocket, Response response) {
this.openingWS = false; // already open
this.webSocket = webSocket; // storing websocket for future usages
if (listener != null) listener.onWSOpen();
}
public void sendCommand(String cmd) {
System.out.println("SEND " + cmd);
if (webSocket != null) webSocket.send(cmd);
}
这个类正在实现拦截器
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
if (!isSpecial()) return chain.proceed(originalRequest);
okhttp3.Request.Builder builder = originalRequest.newBuilder()
.addHeader("text", "...")
.addHeader("dfds", "...");
Request compressedRequest = builder.build();
return chain.proceed(compressedRequest);
}
标头中发送的身份验证代码将每X秒/分钟更改一次。
如果无法动态更改标头,那么实现这种连接的最佳方法是什么
感谢您的帮助。我认为只有在您请求连接时才会第一次发送标题,以后取决于客户端和服务器之间的帧 因此,如果您想通知服务器您已经更改了头,那么就用新头发送消息。或者,您可以关闭连接并使用新标头启动新连接