Expose Connection Id in SignalR TS Client (#9188)

This commit is contained in:
Mikael Mengistu 2019-04-09 12:56:07 -07:00 committed by GitHub
parent cacf9aa9f1
commit 454ac15443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 0 deletions

View File

@ -34,6 +34,11 @@ namespace FunctionalTests
return message;
}
public string GetCallerConnectionId()
{
return Context.ConnectionId;
}
public int GetNumRedirects()
{
return int.Parse(Context.GetHttpContext().Request.Query["numRedirects"]);

View File

@ -782,6 +782,9 @@ describe("hubConnection", () => {
expect(newConnectionId).not.toBe(initialConnectionId);
const serverConnectionId = await hubConnection.invoke<string>("GetCallerConnectionId");
expect(newConnectionId).toBe(serverConnectionId);
const postReconnectRedirects = await hubConnection.invoke<number>("GetNumRedirects");
expect(postReconnectRedirects).toBeGreaterThan(preReconnectRedirects);
@ -838,6 +841,79 @@ describe("hubConnection", () => {
}
});
it("connection id matches server side connection id", async (done) => {
try {
const reconnectingPromise = new PromiseSource();
const reconnectedPromise = new PromiseSource<string | undefined>();
const hubConnection = getConnectionBuilder(undefined, TESTHUB_REDIRECT_ENDPOINT_URL)
.withAutomaticReconnect()
.build();
hubConnection.onreconnecting(() => {
reconnectingPromise.resolve();
});
hubConnection.onreconnected((connectionId?) => {
reconnectedPromise.resolve(connectionId);
});
expect(hubConnection.connectionId).toBeNull();
await hubConnection.start();
expect(hubConnection.connectionId).not.toBeNull();
let serverConnectionId = await hubConnection.invoke<string>("GetCallerConnectionId");
expect(hubConnection.connectionId).toBe(serverConnectionId);
const initialConnectionId = hubConnection.connectionId!;
// Induce reconnect
(hubConnection as any).serverTimeout();
await reconnectingPromise;
const newConnectionId = await reconnectedPromise;
expect(newConnectionId).not.toBe(initialConnectionId);
serverConnectionId = await hubConnection.invoke<string>("GetCallerConnectionId");
expect(newConnectionId).toBe(serverConnectionId);
await hubConnection.stop();
expect(hubConnection.connectionId).toBeNull();
done();
} catch (err) {
fail(err);
done();
}
});
it("connection id is alwys null is negotiation is skipped", async (done) => {
try {
const hubConnection = getConnectionBuilder(
HttpTransportType.WebSockets,
undefined,
{ skipNegotiation: true },
)
.build();
expect(hubConnection.connectionId).toBeNull();
await hubConnection.start();
expect(hubConnection.connectionId).toBeNull();
await hubConnection.stop();
expect(hubConnection.connectionId).toBeNull();
done();
} catch (err) {
fail(err);
done();
}
});
if (typeof EventSource !== "undefined") {
it("allows Server-Sent Events when negotiating for JSON protocol", async (done) => {
const hubConnection = getConnectionBuilder(undefined, TESTHUB_NOWEBSOCKETS_ENDPOINT_URL)

View File

@ -450,6 +450,7 @@ export class HttpConnection implements IConnection {
this.logger.log(LogLevel.Information, "Connection disconnected.");
}
this.connectionId = undefined;
this.connectionState = ConnectionState.Disconnected;
if (this.onclose && this.connectionStarted) {

View File

@ -120,6 +120,13 @@ export class HubConnection {
return this.connectionState;
}
/** Represents the connection id of the {@link HubConnection} on the server. The connection id will be null when the connection is either
* in the disconnected state or if the negotiation step was skipped.
*/
get connectionId(): string | null {
return this.connection ? (this.connection.connectionId || null) : null;
}
/** Starts the connection.
*
* @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.