@@ -35,22 +35,38 @@ public class WebSocketHubConnection implements HubConnection {
3535 private String hubUrl ;
3636 private Gson gson = new Gson ();
3737
38- public WebSocketHubConnection (String hubUrl ) {
38+ private String connectionId = null ;
39+ private String authHeader = null ;
40+
41+ public WebSocketHubConnection (String hubUrl , String authHeader ) {
3942 this .hubUrl = hubUrl ;
43+ this .authHeader = authHeader ;
4044 parsedUri = Uri .parse (hubUrl );
4145 }
4246
4347 @ Override
44- public void connect (final String authHeader ) {
45- Runnable runnable = new Runnable () {
46- public void run () {
47- getConnectionId (authHeader );
48- }
49- };
48+ public synchronized void connect () {
49+ if (client != null && (client .isOpen () || client .isConnecting ()))
50+ return ;
51+
52+ Runnable runnable ;
53+ if (connectionId == null ) {
54+ runnable = new Runnable () {
55+ public void run () {
56+ getConnectionId ();
57+ }
58+ };
59+ } else {
60+ runnable = new Runnable () {
61+ public void run () {
62+ connectClient ();
63+ }
64+ };
65+ }
5066 new Thread (runnable ).start ();
5167 }
5268
53- private void getConnectionId (String authHeader ) {
69+ private void getConnectionId () {
5470 Log .i (TAG , "Requesting connection id..." );
5571 if (!(parsedUri .getScheme ().equals ("http" ) || parsedUri .getScheme ().equals ("https" )))
5672 throw new RuntimeException ("URL must start with http or https" );
@@ -75,18 +91,23 @@ private void getConnectionId(String authHeader) {
7591 if (!availableTransports .contains ("WebSockets" )) {
7692 throw new RuntimeException ("The server does not support WebSockets transport" );
7793 }
78- connectClient (connectionId , authHeader );
94+ this .connectionId = connectionId ;
95+ connectClient ();
7996 } else if (responseCode == 401 ) {
80- throw new RuntimeException ("Unauthorized request" );
97+ RuntimeException runtimeException = new RuntimeException ("Unauthorized request" );
98+ error (runtimeException );
99+ throw runtimeException ;
81100 } else {
82- throw new RuntimeException ("Server error" );
101+ RuntimeException runtimeException = new RuntimeException ("Server error" );
102+ error (runtimeException );
103+ throw runtimeException ;
83104 }
84105 } catch (Exception e ) {
85106 e .printStackTrace ();
86107 }
87108 }
88109
89- private void connectClient (String connectionId , String authHeader ) throws Exception {
110+ private void connectClient () {
90111 Uri .Builder uriBuilder = parsedUri .buildUpon ();
91112 uriBuilder .appendQueryParameter ("id" , connectionId );
92113 uriBuilder .scheme (parsedUri .getScheme ().replace ("http" , "ws" ));
@@ -95,49 +116,55 @@ private void connectClient(String connectionId, String authHeader) throws Except
95116 if (authHeader != null && !authHeader .isEmpty ()) {
96117 headers .put ("Authorization" , authHeader );
97118 }
98- client = new WebSocketClient (new URI (uri .toString ()), new Draft_6455 (), headers , 15000 ) {
99- @ Override
100- public void onOpen (ServerHandshake handshakedata ) {
101- Log .i (TAG , "Opened" );
102- for (HubConnectionListener listener : listeners ) {
103- listener .onConnected ();
104- }
105- send ("{\" protocol\" :\" json\" }" + SPECIAL_SYMBOL );
106- }
107-
108- @ Override
109- public void onMessage (String message ) {
110- Log .i (TAG , message );
111- SignalRMessage element = gson .fromJson (message .replace (SPECIAL_SYMBOL , "" ), SignalRMessage .class );
112- if (element .getType () == 1 ) {
113- HubMessage hubMessage = new HubMessage (element .getInvocationId (), element .getTarget (), element .getArguments ());
119+ try {
120+ client = new WebSocketClient (new URI (uri .toString ()), new Draft_6455 (), headers , 15000 ) {
121+ @ Override
122+ public void onOpen (ServerHandshake handshakedata ) {
123+ Log .i (TAG , "Opened" );
114124 for (HubConnectionListener listener : listeners ) {
115- listener .onMessage ( hubMessage );
125+ listener .onConnected ( );
116126 }
127+ send ("{\" protocol\" :\" json\" }" + SPECIAL_SYMBOL );
128+ }
129+
130+ @ Override
131+ public void onMessage (String message ) {
132+ Log .i (TAG , message );
133+ SignalRMessage element = gson .fromJson (message .replace (SPECIAL_SYMBOL , "" ), SignalRMessage .class );
134+ if (element .getType () == 1 ) {
135+ HubMessage hubMessage = new HubMessage (element .getInvocationId (), element .getTarget (), element .getArguments ());
136+ for (HubConnectionListener listener : listeners ) {
137+ listener .onMessage (hubMessage );
138+ }
117139
118- List <HubEventListener > hubEventListeners = eventListeners .get (hubMessage .getTarget ());
119- if (hubEventListeners != null ) {
120- for (HubEventListener listener : hubEventListeners ) {
121- listener .onEventMessage (hubMessage );
140+ List <HubEventListener > hubEventListeners = eventListeners .get (hubMessage .getTarget ());
141+ if (hubEventListeners != null ) {
142+ for (HubEventListener listener : hubEventListeners ) {
143+ listener .onEventMessage (hubMessage );
144+ }
122145 }
123146 }
124147 }
125- }
126148
127- @ Override
128- public void onClose (int code , String reason , boolean remote ) {
129- Log .i (TAG , String .format ("Closed. Code: %s, Reason: %s, Remote: %s" , code , reason , remote ));
130- for (HubConnectionListener listener : listeners ) {
131- listener .onDisconnected ();
149+ @ Override
150+ public void onClose (int code , String reason , boolean remote ) {
151+ Log .i (TAG , String .format ("Closed. Code: %s, Reason: %s, Remote: %s" , code , reason , remote ));
152+ for (HubConnectionListener listener : listeners ) {
153+ listener .onDisconnected ();
154+ }
155+ connectionId = null ;
132156 }
133- }
134157
135- @ Override
136- public void onError (Exception ex ) {
137- Log .i (TAG , "Error " + ex .getMessage ());
138- error (ex );
139- }
140- };
158+ @ Override
159+ public void onError (Exception ex ) {
160+ Log .i (TAG , "Error " + ex .getMessage ());
161+ error (ex );
162+ }
163+ };
164+ } catch (Exception e ) {
165+ error (e );
166+ }
167+
141168 Log .i (TAG , "Connecting..." );
142169 client .connect ();
143170 }
0 commit comments