Adding HubConnection State Enum (#2627)

This commit is contained in:
Mikael Mengistu 2018-07-16 14:18:55 -07:00 committed by GitHub
parent a2942ce885
commit 3e53533db3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 6 deletions

View File

@ -15,8 +15,7 @@ public class HubConnection {
private CallbackMap handlers = new CallbackMap(); private CallbackMap handlers = new CallbackMap();
private HubProtocol protocol; private HubProtocol protocol;
private Gson gson = new Gson(); private Gson gson = new Gson();
private HubConnectionState connectionState = HubConnectionState.DISCONNECTED;
public Boolean connected = false;
public HubConnection(String url, Transport transport){ public HubConnection(String url, Transport transport){
this.url = url; this.url = url;
@ -68,15 +67,19 @@ public class HubConnection {
this(url, null); this(url, null);
} }
public HubConnectionState getConnectionState() {
return connectionState;
}
public void start() throws InterruptedException { public void start() throws InterruptedException {
transport.setOnReceive(this.callback); transport.setOnReceive(this.callback);
transport.start(); transport.start();
connected = true; connectionState = HubConnectionState.CONNECTED;
} }
public void stop(){ public void stop(){
transport.stop(); transport.stop();
connected = false; connectionState = HubConnectionState.DISCONNECTED;
} }
public void send(String method, Object... args) throws Exception { public void send(String method, Object... args) throws Exception {

View File

@ -0,0 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
public enum HubConnectionState {
CONNECTED,
DISCONNECTED,
}

View File

@ -13,10 +13,10 @@ public class HubConnectionTest {
Transport mockTransport = new MockEchoTransport(); Transport mockTransport = new MockEchoTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport); HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
hubConnection.start(); hubConnection.start();
assertTrue(hubConnection.connected); assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
hubConnection.stop(); hubConnection.stop();
assertFalse(hubConnection.connected); assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
} }
@Test @Test