Skip to content

Add support for negotiateVersion query string parameter #13880

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

Merged
merged 9 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class HubConnection {
private Map<String, Observable> streamMap = new ConcurrentHashMap<>();
private TransportEnum transportEnum = TransportEnum.ALL;
private String connectionId;
private String connectionToken;
private final int negotiateVersion = 1;
private final Logger logger = LoggerFactory.getLogger(HubConnection.class);

/**
Expand Down Expand Up @@ -340,11 +342,12 @@ public Completable start() {
});

stopError = null;
String urlWithQS = Utils.appendQueryString(baseUrl, "negotiateVersion=" + negotiateVersion);
Single<NegotiateResponse> negotiate = null;
if (!skipNegotiate) {
negotiate = tokenCompletable.andThen(Single.defer(() -> startNegotiate(baseUrl, 0)));
negotiate = tokenCompletable.andThen(Single.defer(() -> startNegotiate(urlWithQS, 0)));
} else {
negotiate = tokenCompletable.andThen(Single.defer(() -> Single.just(new NegotiateResponse(baseUrl))));
negotiate = tokenCompletable.andThen(Single.defer(() -> Single.just(new NegotiateResponse(urlWithQS))));
}

CompletableSubject start = CompletableSubject.create();
Expand Down Expand Up @@ -377,7 +380,6 @@ public Completable start() {
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.
Expand Down Expand Up @@ -447,19 +449,21 @@ private Single<NegotiateResponse> startNegotiate(String url, int negotiateAttemp
throw new RuntimeException("There were no compatible transports on the server.");
}

String finalUrl = url;
if (response.getConnectionId() != null) {
if (url.contains("?")) {
finalUrl = url + "&id=" + response.getConnectionId();
} else {
finalUrl = url + "?id=" + response.getConnectionId();
}
if (response.getVersion() > 0) {
this.connectionId = response.getConnectionId();
this.connectionToken = response.getConnectionToken();
} else {
this.connectionToken = this.connectionId = response.getConnectionId();
}

String finalUrl = Utils.appendQueryString(url, "id=" + this.connectionToken);

response.setFinalUrl(finalUrl);
return Single.just(response);
}

return startNegotiate(response.getRedirectUrl(), negotiateAttempts + 1);
String redirectUrl = Utils.appendQueryString(response.getRedirectUrl(), "negotiateVersion=" + negotiateVersion);
return startNegotiate(redirectUrl, negotiateAttempts + 1);
});
}

Expand Down Expand Up @@ -521,6 +525,7 @@ private void stopConnection(String errorMessage) {
handshakeResponseSubject.onComplete();
redirectAccessTokenProvider = null;
connectionId = null;
connectionToken = null;
Copy link
Member

Choose a reason for hiding this comment

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

Are we trying to follow any convention related to using or not using this. to access fields?

Copy link
Member

Choose a reason for hiding this comment

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

We should, but not currently. It's just random right now.

transportEnum = TransportEnum.ALL;
this.localHeaders.clear();
this.streamMap.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static String resolveNegotiateUrl(String url) {
// Check if we have a query string. If we do then we ignore it for now.
int queryStringIndex = url.indexOf('?');
if (queryStringIndex > 0) {
negotiateUrl = url.substring(0, url.indexOf('?'));
negotiateUrl = url.substring(0, queryStringIndex);
} else {
negotiateUrl = url;
}
Expand All @@ -24,7 +24,7 @@ public static String resolveNegotiateUrl(String url) {

// Add the query string back if it existed.
if (queryStringIndex > 0) {
negotiateUrl += url.substring(url.indexOf('?'));
negotiateUrl += url.substring(queryStringIndex);
}

return negotiateUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

class NegotiateResponse {
private String connectionId;
private String connectionToken;
private Set<String> availableTransports = new HashSet<>();
private String redirectUrl;
private String accessToken;
private String error;
private String finalUrl;
private int version;

public NegotiateResponse(JsonReader reader) {
try {
Expand All @@ -30,6 +32,12 @@ public NegotiateResponse(JsonReader reader) {
case "ProtocolVersion":
this.error = "Detected an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server. See https://aka.ms/signalr-core-differences for details.";
return;
case "negotiateVersion":
this.version = reader.nextInt();
break;
case "connectionToken":
this.connectionToken = reader.nextString();
break;
case "url":
this.redirectUrl = reader.nextString();
break;
Expand Down Expand Up @@ -106,6 +114,14 @@ public String getFinalUrl() {
return finalUrl;
}

public int getVersion() {
return version;
}

public String getConnectionToken() {
return connectionToken;
}

public void setFinalUrl(String url) {
this.finalUrl = url;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

package com.microsoft.signalr;

class Utils {
public static String appendQueryString(String original, String queryStringValue) {
if (original.contains("?")) {
return original + "&" + queryStringValue;
} else {
return original + "?" + queryStringValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// 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.

Expand Down
Loading