Expose Connection Id in SignalR TS Client (#9188)
This commit is contained in:
parent
cacf9aa9f1
commit
454ac15443
|
|
@ -34,6 +34,11 @@ namespace FunctionalTests
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetCallerConnectionId()
|
||||||
|
{
|
||||||
|
return Context.ConnectionId;
|
||||||
|
}
|
||||||
|
|
||||||
public int GetNumRedirects()
|
public int GetNumRedirects()
|
||||||
{
|
{
|
||||||
return int.Parse(Context.GetHttpContext().Request.Query["numRedirects"]);
|
return int.Parse(Context.GetHttpContext().Request.Query["numRedirects"]);
|
||||||
|
|
|
||||||
|
|
@ -782,6 +782,9 @@ describe("hubConnection", () => {
|
||||||
|
|
||||||
expect(newConnectionId).not.toBe(initialConnectionId);
|
expect(newConnectionId).not.toBe(initialConnectionId);
|
||||||
|
|
||||||
|
const serverConnectionId = await hubConnection.invoke<string>("GetCallerConnectionId");
|
||||||
|
expect(newConnectionId).toBe(serverConnectionId);
|
||||||
|
|
||||||
const postReconnectRedirects = await hubConnection.invoke<number>("GetNumRedirects");
|
const postReconnectRedirects = await hubConnection.invoke<number>("GetNumRedirects");
|
||||||
|
|
||||||
expect(postReconnectRedirects).toBeGreaterThan(preReconnectRedirects);
|
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") {
|
if (typeof EventSource !== "undefined") {
|
||||||
it("allows Server-Sent Events when negotiating for JSON protocol", async (done) => {
|
it("allows Server-Sent Events when negotiating for JSON protocol", async (done) => {
|
||||||
const hubConnection = getConnectionBuilder(undefined, TESTHUB_NOWEBSOCKETS_ENDPOINT_URL)
|
const hubConnection = getConnectionBuilder(undefined, TESTHUB_NOWEBSOCKETS_ENDPOINT_URL)
|
||||||
|
|
|
||||||
|
|
@ -450,6 +450,7 @@ export class HttpConnection implements IConnection {
|
||||||
this.logger.log(LogLevel.Information, "Connection disconnected.");
|
this.logger.log(LogLevel.Information, "Connection disconnected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.connectionId = undefined;
|
||||||
this.connectionState = ConnectionState.Disconnected;
|
this.connectionState = ConnectionState.Disconnected;
|
||||||
|
|
||||||
if (this.onclose && this.connectionStarted) {
|
if (this.onclose && this.connectionStarted) {
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,13 @@ export class HubConnection {
|
||||||
return this.connectionState;
|
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.
|
/** Starts the connection.
|
||||||
*
|
*
|
||||||
* @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
|
* @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue