|
36 | 36 | import java.util.Collection; |
37 | 37 | import java.util.Collections; |
38 | 38 | import java.util.Map; |
| 39 | +import java.util.TreeMap; |
39 | 40 | import java.util.concurrent.CountDownLatch; |
40 | 41 | import java.util.concurrent.TimeUnit; |
41 | 42 |
|
@@ -194,7 +195,10 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String> |
194 | 195 | } |
195 | 196 | this.uri = serverUri; |
196 | 197 | 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 | + } |
198 | 202 | this.connectTimeout = connectTimeout; |
199 | 203 | setTcpNoDelay( false ); |
200 | 204 | setReuseAddr( false ); |
@@ -226,6 +230,42 @@ public Socket getSocket() { |
226 | 230 | return socket; |
227 | 231 | } |
228 | 232 |
|
| 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 | + |
229 | 269 | /** |
230 | 270 | * Reinitiates the websocket connection. This method does not block. |
231 | 271 | * @since 1.3.8 |
|
0 commit comments