Skip to content

Commit 36a8102

Browse files
authored
Merge pull request TooTallNate#868 from haruntuncay/issue#865
Add a way to put additional headers to handshake for connecting/reconnecting, see TooTallNate#865
2 parents 9293102 + 1e9d6f0 commit 36a8102

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Collection;
3737
import java.util.Collections;
3838
import java.util.Map;
39+
import java.util.TreeMap;
3940
import java.util.concurrent.CountDownLatch;
4041
import java.util.concurrent.TimeUnit;
4142

@@ -194,7 +195,10 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String>
194195
}
195196
this.uri = serverUri;
196197
this.draft = protocolDraft;
197-
this.headers = httpHeaders;
198+
if(httpHeaders != null) {
199+
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
200+
headers.putAll(httpHeaders);
201+
}
198202
this.connectTimeout = connectTimeout;
199203
setTcpNoDelay( false );
200204
setReuseAddr( false );
@@ -226,6 +230,42 @@ public Socket getSocket() {
226230
return socket;
227231
}
228232

233+
/**
234+
* @since 1.4.1
235+
* Adds an additional header to be sent in the handshake.<br>
236+
* If the connection is already made, adding headers has no effect,
237+
* unless reconnect is called, which then a new handshake is sent.<br>
238+
* If a header with the same key already exists, it is overridden.
239+
* @param key Name of the header to add.
240+
* @param value Value of the header to add.
241+
*/
242+
public void addHeader(String key, String value){
243+
if(headers == null)
244+
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
245+
headers.put(key, value);
246+
}
247+
248+
/**
249+
* @since 1.4.1
250+
* Removes a header from the handshake to be sent, if header key exists.<br>
251+
* @param key Name of the header to remove.
252+
* @return the previous value associated with key, or
253+
* null if there was no mapping for key.
254+
*/
255+
public String removeHeader(String key) {
256+
if(headers == null)
257+
return null;
258+
return headers.remove(key);
259+
}
260+
261+
/**
262+
* @since 1.4.1
263+
* Clears all previously put headers.
264+
*/
265+
public void clearHeaders() {
266+
headers = null;
267+
}
268+
229269
/**
230270
* Reinitiates the websocket connection. This method does not block.
231271
* @since 1.3.8

0 commit comments

Comments
 (0)