2017-02-28 14 views
0

私はwebsocketを作成し、送信されたすべてのメッセージでそのヘッダを動的に再計算しようとしています。出来ますか?okhttp3 websocket dynamic header

私はインターセプタを使用しようとしていましたが、一度しか呼び出されませんでした。

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秒ごとに変化するヘッダで送信された認証コードを実施しています。 ヘッダーを動的に変更できない場合は、このような接続にはどのような方法が最適ですか?

ありがとうございました。

答えて

0

私はヘッダーが最初にあなたが接続を要求するときに送信すると思う、後ではクライアントとサーバーの間のフレームに依存します。

したがって、ヘッダーを変更したことをサーバーに通知したい場合は、新しいヘッダーでメッセージを送信します。または、接続を閉じて、新しいヘッダーで新しい接続を開始することもできます。

+0

これは質問に対する答えを提供しません。十分な[評判](https://stackoverflow.com/help/whats-reputation)があれば、[投稿にコメントする]ことができます(https://stackoverflow.com/help/privileges/comment)。代わりに、[質問者からの明確化を必要としない回答を提供する](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- i-do-代わりに)。 - [レビューから](/レビュー/低品質の投稿/ 17561469) –