Skip to content

Commit f00ebef

Browse files
author
Noah Andrews
committed
2 parents 509b1cf + 36a8102 commit f00ebef

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,13 @@
224224
<dependency>
225225
<groupId>junit</groupId>
226226
<artifactId>junit</artifactId>
227-
<version>4.11</version>
227+
<version>4.12</version>
228228
<scope>test</scope>
229229
</dependency>
230230
<dependency>
231231
<groupId>org.json</groupId>
232232
<artifactId>json</artifactId>
233-
<version>20180130</version>
233+
<version>20180813</version>
234234
<scope>test</scope>
235235
</dependency>
236236
</dependencies>

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)