diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 1e6c5d8e..fc86d813 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -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; @@ -194,7 +195,10 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map } this.uri = serverUri; this.draft = protocolDraft; - this.headers = httpHeaders; + if(httpHeaders != null) { + headers = new TreeMap(String.CASE_INSENSITIVE_ORDER); + headers.putAll(httpHeaders); + } this.connectTimeout = connectTimeout; setTcpNoDelay( false ); setReuseAddr( false ); @@ -226,6 +230,42 @@ public Socket getSocket() { return socket; } + /** + * @since 1.4.1 + * Adds an additional header to be sent in the handshake.
+ * If the connection is already made, adding headers has no effect, + * unless reconnect is called, which then a new handshake is sent.
+ * 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.CASE_INSENSITIVE_ORDER); + headers.put(key, value); + } + + /** + * @since 1.4.1 + * Removes a header from the handshake to be sent, if header key exists.
+ * @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