Skip to content

Commit

Permalink
Merge pull request #868 from haruntuncay/issue#865
Browse files Browse the repository at this point in the history
Add a way to put additional headers to handshake for connecting/reconnecting, see #865
  • Loading branch information
marci4 committed Apr 8, 2019
2 parents 9293102 + 1e9d6f0 commit 36a8102
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/main/java/org/java_websocket/client/WebSocketClient.java
Expand Up @@ -36,6 +36,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -194,7 +195,10 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String>
}
this.uri = serverUri;
this.draft = protocolDraft;
this.headers = httpHeaders;
if(httpHeaders != null) {
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.putAll(httpHeaders);
}
this.connectTimeout = connectTimeout;
setTcpNoDelay( false );
setReuseAddr( false );
Expand Down Expand Up @@ -226,6 +230,42 @@ public Socket getSocket() {
return socket;
}

/**
* @since 1.4.1
* Adds an additional header to be sent in the handshake.<br>
* If the connection is already made, adding headers has no effect,
* unless reconnect is called, which then a new handshake is sent.<br>
* If a header with the same key already exists, it is overridden.
* @param key Name of the header to add.
* @param value Value of the header to add.
*/
public void addHeader(String key, String value){
if(headers == null)
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.put(key, value);
}

/**
* @since 1.4.1
* Removes a header from the handshake to be sent, if header key exists.<br>
* @param key Name of the header to remove.
* @return the previous value associated with key, or
* null if there was no mapping for key.
*/
public String removeHeader(String key) {
if(headers == null)
return null;
return headers.remove(key);
}

/**
* @since 1.4.1
* Clears all previously put headers.
*/
public void clearHeaders() {
headers = null;
}

/**
* Reinitiates the websocket connection. This method does not block.
* @since 1.3.8
Expand Down

0 comments on commit 36a8102

Please sign in to comment.