From f12c088504f95b757ca5548af273b278d822baa4 Mon Sep 17 00:00:00 2001 From: haruntuncay Date: Thu, 14 Mar 2019 17:06:05 +0300 Subject: [PATCH 1/2] Add a way to put additional headers to handshake for connecting/reconnecting, see #865 --- .../client/WebSocketClient.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 1e6c5d8e..63b5ace3 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; @@ -226,6 +227,36 @@ public Socket getSocket() { return socket; } + /** + * 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. + */ + public void addHeader(String key, String value){ + if(headers == null) + headers = new TreeMap(String.CASE_INSENSITIVE_ORDER); + headers.put(key, value); + } + + /** + * Removes a header from the handshake to be sent, if header key exists.
+ * @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); + } + + /** + * Clears all previously put headers. + */ + public void clearHeaders() { + headers = null; + } + /** * Reinitiates the websocket connection. This method does not block. * @since 1.3.8 From 1e9d6f02381b37f108b09fcb1e8a382644fdcea8 Mon Sep 17 00:00:00 2001 From: haruntuncay Date: Sun, 7 Apr 2019 22:29:16 +0300 Subject: [PATCH 2/2] Fix PR review changes. --- .../org/java_websocket/client/WebSocketClient.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 63b5ace3..fc86d813 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -195,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 ); @@ -228,10 +231,13 @@ public Socket getSocket() { } /** + * @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) @@ -240,7 +246,9 @@ public void addHeader(String key, String 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. */ @@ -251,6 +259,7 @@ public String removeHeader(String key) { } /** + * @since 1.4.1 * Clears all previously put headers. */ public void clearHeaders() {