Skip to content

Commit

Permalink
Add ability to change url in TS client (dotnet#11102)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelm12 authored Jun 13, 2019
1 parent 14e7aca commit 7932622
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Components/Browser.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions src/SignalR/clients/ts/FunctionalTests/ts/HubConnectionTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,36 @@ describe("hubConnection", () => {
}
});
});

it("can change url in reconnecting state", async (done) => {
try {
const reconnectingPromise = new PromiseSource();
const hubConnection = getConnectionBuilder(transportType)
.withAutomaticReconnect()
.build();

hubConnection.onreconnecting(() => {
hubConnection.baseUrl = "http://example123.com";
reconnectingPromise.resolve();
});

await hubConnection.start();

// Induce reconnect
(hubConnection as any).serverTimeout();

await reconnectingPromise;

expect(hubConnection.baseUrl).toBe("http://example123.com");

await hubConnection.stop();

done();
} catch (err) {
fail(err);
done();
}
});
});

it("can reconnect after negotiate redirect", async (done) => {
Expand Down
2 changes: 1 addition & 1 deletion src/SignalR/clients/ts/signalr/src/HttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class HttpConnection implements IConnection {
// connectionStarted is tracked independently from connectionState, so we can check if the
// connection ever did successfully transition from connecting to connected before disconnecting.
private connectionStarted: boolean;
private readonly baseUrl: string;
private readonly httpClient: HttpClient;
private readonly logger: ILogger;
private readonly options: IHttpConnectionOptions;
Expand All @@ -66,6 +65,7 @@ export class HttpConnection implements IConnection {
private sendQueue?: TransportSendQueue;

public readonly features: any = {};
public baseUrl: string;
public connectionId?: string;
public onreceive: ((data: string | ArrayBuffer) => void) | null;
public onclose: ((e?: Error) => void) | null;
Expand Down
22 changes: 22 additions & 0 deletions src/SignalR/clients/ts/signalr/src/HubConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ export class HubConnection {
return this.connection ? (this.connection.connectionId || null) : null;
}

/** Indicates the url of the {@link HubConnection} to the server. */
get baseUrl(): string {
return this.connection.baseUrl || "";
}

/**
* Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or
* Reconnecting states.
* @param {string} url The url to connect to.
*/
set baseUrl(url: string) {
if (this.connectionState !== HubConnectionState.Disconnected && this.connectionState !== HubConnectionState.Reconnecting) {
throw new Error("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");
}

if (!url) {
throw new Error("The HubConnection url must be a valid url.");
}

this.connection.baseUrl = url;
}

/** Starts the connection.
*
* @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
Expand Down
2 changes: 2 additions & 0 deletions src/SignalR/clients/ts/signalr/src/IConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface IConnection {
readonly features: any;
readonly connectionId?: string;

baseUrl: string;

start(transferFormat: TransferFormat): Promise<void>;
send(data: string | ArrayBuffer): Promise<void>;
stop(error?: Error): Promise<void>;
Expand Down
54 changes: 53 additions & 1 deletion src/SignalR/clients/ts/signalr/tests/HubConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,58 @@ describe("HubConnection", () => {
});
});

it("can change url", async () => {
await VerifyLogger.run(async (logger) => {
const connection = new TestConnection();
const hubConnection = createHubConnection(connection, logger);
try {
await hubConnection.start();
await hubConnection.stop();
hubConnection.baseUrl = "http://newurl.com";
expect(hubConnection.baseUrl).toBe("http://newurl.com");
} finally {
await hubConnection.stop();
}
});
});

it("can change url in onclose", async () => {
await VerifyLogger.run(async (logger) => {
const connection = new TestConnection();
const hubConnection = createHubConnection(connection, logger);
try {
await hubConnection.start();

expect(hubConnection.baseUrl).toBe("http://example.com");
hubConnection.onclose(() => {
hubConnection.baseUrl = "http://newurl.com";
});

await hubConnection.stop();
expect(hubConnection.baseUrl).toBe("http://newurl.com");
} finally {
await hubConnection.stop();
}
});
});

it("changing url while active throws", async () => {
await VerifyLogger.run(async (logger) => {
const connection = new TestConnection();
const hubConnection = createHubConnection(connection, logger);
try {
await hubConnection.start();

expect(() => {
hubConnection.baseUrl = "http://newurl.com";
}).toThrow("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");

} finally {
await hubConnection.stop();
}
});
});

it("state connected", async () => {
await VerifyLogger.run(async (logger) => {
const connection = new TestConnection();
Expand Down Expand Up @@ -1076,7 +1128,7 @@ describe("HubConnection", () => {
hubConnection.stream("testMethod").subscribe(NullSubscriber.instance);

// Send completion to trigger observer.complete()
// Expectation is connection.receive will not to throw
// Expectation is connection.receive will not throw
connection.receive({ type: MessageType.Completion, invocationId: connection.lastInvocationId });
} finally {
await hubConnection.stop();
Expand Down
2 changes: 2 additions & 0 deletions src/SignalR/clients/ts/signalr/tests/TestConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IConnection } from "../src/IConnection";
import { TextMessageFormat } from "../src/TextMessageFormat";

export class TestConnection implements IConnection {
public baseUrl: string;
public readonly features: any = {};
public connectionId?: string;

Expand All @@ -22,6 +23,7 @@ export class TestConnection implements IConnection {
this.sentData = [];
this.lastInvocationId = null;
this.autoHandshake = autoHandshake;
this.baseUrl = "http://example.com";
}

public start(): Promise<void> {
Expand Down

0 comments on commit 7932622

Please sign in to comment.