Skip to content

Commit

Permalink
Support connect HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
NaikSoftware committed May 12, 2016
1 parent 3adaa2f commit 54dfd7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
21 changes: 17 additions & 4 deletions lib/src/main/java/ua/naiksoftware/stomp/Stomp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.java_websocket.WebSocket;

import java.util.Map;

import ua.naiksoftware.stomp.client.StompClient;

/**
Expand All @@ -15,12 +17,23 @@
*/
public class Stomp {

public static StompClient over(Class claszz, String uri) {
if (claszz == WebSocket.class) {
return createStompClient(new WebSocketsConnectionProvider(uri));
public static StompClient over(Class clazz, String uri) {
return over(clazz, uri, null);
}

/**
*
* @param clazz class for using as transport
* @param uri URI to connect
* @param connectHttpHeaders HTTP headers, will be passed with handshake query, may be null
* @return StompClient for receiving and sending messages. Call #StompClient.connect
*/
public static StompClient over(Class clazz, String uri, Map<String, String> connectHttpHeaders) {
if (clazz == WebSocket.class) {
return createStompClient(new WebSocketsConnectionProvider(uri, connectHttpHeaders));
}

throw new RuntimeException("Not supported overlay transport: " + claszz.getName());
throw new RuntimeException("Not supported overlay transport: " + clazz.getName());
}

private static StompClient createStompClient(ConnectionProvider connectionProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
Expand All @@ -21,16 +23,19 @@ public class WebSocketsConnectionProvider implements ConnectionProvider {
private static final String TAG = WebSocketsConnectionProvider.class.getSimpleName();

private final String mUri;
private final Map<String, String> mConnectHttpHeaders;
private WebSocketClient mWebSocketClient;
private List<Subscriber<? super LifecycleEvent>> mLifecycleSubscribers;
private List<Subscriber<? super String>> mMessagesSubscribers;
private boolean haveConnection;

/**
* Support UIR scheme ws://host:port/path
* @param connectHttpHeaders may be null
*/
public WebSocketsConnectionProvider(String uri) {
public WebSocketsConnectionProvider(String uri, Map<String, String> connectHttpHeaders) {
mUri = uri;
mConnectHttpHeaders = connectHttpHeaders != null ? connectHttpHeaders : new HashMap<>();
mLifecycleSubscribers = new ArrayList<>();
mMessagesSubscribers = new ArrayList<>();
}
Expand All @@ -56,7 +61,7 @@ private void createWebSocketConnection() {
if (haveConnection)
throw new IllegalStateException("Already have connection to web socket");

mWebSocketClient = new WebSocketClient(URI.create(mUri), new Draft_17()) {
mWebSocketClient = new WebSocketClient(URI.create(mUri), new Draft_17(), mConnectHttpHeaders, 0) {
@Override
public void onOpen(ServerHandshake handshakeData) {
emitLifecycleEvent(new LifecycleEvent(LifecycleEvent.Type.OPENED));
Expand Down

0 comments on commit 54dfd7c

Please sign in to comment.