2017-02-28 3 views
0

나는 웹 소켓을 만들고 동적으로 모든 메시지를 다시 계산하려고합니다. 가능한가?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; 

     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-can- i-do- 대신). - [리뷰에서] (리뷰/저품절 게시물/17561469) –

관련 문제