diff --git a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java index b64d64cae6..0dc5183f61 100644 --- a/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java +++ b/src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java @@ -55,6 +55,7 @@ public class HubConnection { private long handshakeResponseTimeout = 15*1000; private Map streamMap = new ConcurrentHashMap<>(); private TransportEnum transportEnum = TransportEnum.ALL; + private String connectionId; private final Logger logger = LoggerFactory.getLogger(HubConnection.class); /** @@ -93,6 +94,15 @@ public class HubConnection { return this.keepAliveInterval; } + /** + * Gets the connections connectionId. This value will be cleared when the connection is stopped and + * will have a new value every time the connection is successfully started. + * @return A string representing the the client's connectionId. + */ + public String getConnectionId() { + return this.connectionId; + } + // For testing purposes void setTickRate(long tickRateInMilliseconds) { this.tickRate = tickRateInMilliseconds; @@ -338,6 +348,7 @@ public class HubConnection { hubConnectionStateLock.lock(); try { hubConnectionState = HubConnectionState.CONNECTED; + this.connectionId = negotiateResponse.getConnectionId(); logger.info("HubConnection started."); resetServerTimeout(); //Don't send pings if we're using long polling. @@ -480,6 +491,7 @@ public class HubConnection { hubConnectionState = HubConnectionState.DISCONNECTED; handshakeResponseSubject.onComplete(); redirectAccessTokenProvider = null; + connectionId = null; transportEnum = TransportEnum.ALL; this.headers.remove("Authorization"); } finally { diff --git a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java index eb046824ff..919dffe743 100644 --- a/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java +++ b/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java @@ -1453,6 +1453,49 @@ class HubConnectionTest { assertEquals("Negotiate redirection limit exceeded.", exception.getMessage()); } + @Test + public void noConnectionIdWhenSkippingNegotiate() { + MockTransport transport = new MockTransport(); + HubConnection hubConnection = HubConnectionBuilder + .create("http://example.com") + .withTransportImplementation(transport) + .shouldSkipNegotiate(true) + .build(); + + assertNull(hubConnection.getConnectionId()); + hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait(); + assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState()); + assertNull(hubConnection.getConnectionId()); + hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait(); + assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); + assertNull(hubConnection.getConnectionId()); + } + + @Test + public void connectionIdIsAvailableAfterStart() { + TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate", + (req) -> Single.just(new HttpResponse(200, "", + "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\"" + + "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}"))); + + MockTransport transport = new MockTransport(true); + HubConnection hubConnection = HubConnectionBuilder + .create("http://example.com") + .withTransportImplementation(transport) + .withHttpClient(client) + .build(); + + assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); + assertNull(hubConnection.getConnectionId()); + hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait(); + assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState()); + assertEquals("bVOiRPG8-6YiJ6d7ZcTOVQ", hubConnection.getConnectionId()); + + hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait(); + assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); + assertNull(hubConnection.getConnectionId()); + } + @Test public void afterSuccessfulNegotiateConnectsWithWebsocketsTransport() { TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate",