Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to put additional headers to handshake for connecting/reconnecting, see #865 #868

Merged
merged 2 commits into from Apr 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/main/java/org/java_websocket/client/WebSocketClient.java
Expand Up @@ -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;

Expand Down Expand Up @@ -194,7 +195,10 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String>
}
this.uri = serverUri;
this.draft = protocolDraft;
this.headers = httpHeaders;
if(httpHeaders != null) {
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.putAll(httpHeaders);
}
this.connectTimeout = connectTimeout;
setTcpNoDelay( false );
setReuseAddr( false );
Expand Down Expand Up @@ -226,6 +230,42 @@ public Socket getSocket() {
return socket;
}

/**
* @since 1.4.1
* Adds an additional header to be sent in the handshake.<br>
* If the connection is already made, adding headers has no effect,
* unless reconnect is called, which then a new handshake is sent.<br>
* 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.
*/
Copy link
Collaborator

@marci4 marci4 Apr 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add:

  • @since 1.4.1
  • some description what key and value represent

here and for the other 2 new methods

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. Of course, I am on it right now 😃

public void addHeader(String key, String value){
if(headers == null)
headers = new TreeMap<String, String>(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.<br>
* @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
Expand Down