Skip to content

[SignalR][TS client] Send ping on connection start #42905

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -376,7 +376,13 @@ describe("hubConnection", () => {
try {
await hubConnection.start();
} catch (error) {
expect(error!.message).toEqual(expectedErrorMessage);
if (error!.message.includes("404")) {
// SSE can race with the connection closing and the initial ping being successful or failing with a 404.
// LongPolling doesn't have pings and WebSockets is a synchronous API over a single HTTP request so it doesn't have the same issues
expect(error!.message).toEqual("No Connection with that ID: Status code '404'");
} else {
expect(error!.message).toEqual(expectedErrorMessage);
}
closePromise.resolve();
}
await closePromise;
Expand Down
4 changes: 4 additions & 0 deletions src/SignalR/clients/ts/signalr/src/HubConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ export class HubConnection {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw this._stopDuringStartError;
}

if (!this.connection.features.inherentKeepAlive) {
await this._sendMessage(this._cachedPingMessage);
}
} catch (e) {
this._logger.log(LogLevel.Debug, `Hub handshake failed with error '${e}' during start(). Stopping HubConnection.`);

Expand Down
111 changes: 55 additions & 56 deletions src/SignalR/clients/ts/signalr/tests/HubConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("HubConnection", () => {
const hubConnection = createHubConnection(connection, logger);
try {
await hubConnection.start();
expect(connection.sentData.length).toBe(1);
expect(connection.sentData.length).toBe(2);
expect(JSON.parse(connection.sentData[0])).toEqual({
protocol: "json",
version: 1,
Expand Down Expand Up @@ -448,7 +448,7 @@ describe("HubConnection", () => {
const subject = new Subject();
const invokePromise = hubConnection.invoke("testMethod", "arg", subject);

expect(JSON.parse(connection.sentData[1])).toEqual({
expect(JSON.parse(connection.sentData[2])).toEqual({
arguments: ["arg"],
invocationId: "1",
streamIds: ["0"],
Expand All @@ -460,7 +460,7 @@ describe("HubConnection", () => {
await new Promise<void>((resolve) => {
setTimeout(resolve, 50);
});
expect(JSON.parse(connection.sentData[2])).toEqual({
expect(JSON.parse(connection.sentData[3])).toEqual({
invocationId: "0",
item: "item numero uno",
type: MessageType.StreamItem,
Expand All @@ -485,7 +485,7 @@ describe("HubConnection", () => {
const subject = new Subject();
await hubConnection.send("testMethod", "arg", subject);

expect(JSON.parse(connection.sentData[1])).toEqual({
expect(JSON.parse(connection.sentData[2])).toEqual({
arguments: ["arg"],
streamIds: ["0"],
target: "testMethod",
Expand All @@ -496,7 +496,7 @@ describe("HubConnection", () => {
await new Promise<void>((resolve) => {
setTimeout(resolve, 50);
});
expect(JSON.parse(connection.sentData[2])).toEqual({
expect(JSON.parse(connection.sentData[3])).toEqual({
invocationId: "0",
item: "item numero uno",
type: MessageType.StreamItem,
Expand Down Expand Up @@ -528,7 +528,7 @@ describe("HubConnection", () => {
},
});

expect(JSON.parse(connection.sentData[1])).toEqual({
expect(JSON.parse(connection.sentData[2])).toEqual({
arguments: ["arg"],
invocationId: "1",
streamIds: ["0"],
Expand All @@ -540,7 +540,7 @@ describe("HubConnection", () => {
await new Promise<void>((resolve) => {
setTimeout(resolve, 50);
});
expect(JSON.parse(connection.sentData[2])).toEqual({
expect(JSON.parse(connection.sentData[3])).toEqual({
invocationId: "0",
item: "item numero uno",
type: MessageType.StreamItem,
Expand Down Expand Up @@ -1102,10 +1102,10 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].result).toEqual(10);
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].result).toEqual(10);
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1133,10 +1133,10 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].result).toBeNull();
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].result).toBeNull();
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1166,10 +1166,10 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].result).toEqual(13);
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].result).toEqual(13);
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1197,10 +1197,10 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].error).toEqual("Error: from callback");
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].error).toEqual("Error: from callback");
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1229,10 +1229,10 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].error).toEqual('Client provided multiple results.');
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].error).toEqual('Client provided multiple results.');
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1261,11 +1261,11 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].error).toEqual("Error: from callback");
expect(connection.parsedSentData[1].result).toBeUndefined();
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].error).toEqual("Error: from callback");
expect(connection.parsedSentData[2].result).toBeUndefined();
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1294,11 +1294,11 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].result).toEqual(3);
expect(connection.parsedSentData[1].error).toBeUndefined();
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].result).toEqual(3);
expect(connection.parsedSentData[2].error).toBeUndefined();
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1326,10 +1326,10 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].error).toEqual("Client didn't provide a result.");
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].error).toEqual("Client didn't provide a result.");
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand All @@ -1355,10 +1355,10 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(2);
expect(connection.parsedSentData[1].type).toEqual(3);
expect(connection.parsedSentData[1].error).toEqual("Client didn't provide a result.");
expect(connection.parsedSentData[1].invocationId).toEqual("1");
expect(connection.parsedSentData.length).toEqual(3);
expect(connection.parsedSentData[2].type).toEqual(3);
expect(connection.parsedSentData[2].error).toEqual("Client didn't provide a result.");
expect(connection.parsedSentData[2].invocationId).toEqual("1");
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1386,7 +1386,7 @@ describe("HubConnection", () => {
// async here to guarantee the sent message is written
await delayUntil(1);

expect(connection.parsedSentData.length).toEqual(1);
expect(connection.parsedSentData.length).toEqual(2);
} finally {
await hubConnection.stop();
}
Expand All @@ -1405,9 +1405,9 @@ describe("HubConnection", () => {

hubConnection.stream("testStream", "arg", 42);

// Verify the message is sent (+ handshake)
expect(connection.sentData.length).toBe(2);
expect(JSON.parse(connection.sentData[1])).toEqual({
// Verify the message is sent (+ handshake + ping)
expect(connection.sentData.length).toBe(3);
expect(JSON.parse(connection.sentData[2])).toEqual({
arguments: [
"arg",
42,
Expand All @@ -1416,9 +1416,6 @@ describe("HubConnection", () => {
target: "testStream",
type: MessageType.StreamInvocation,
});

// Close the connection
await hubConnection.stop();
} finally {
await hubConnection.stop();
}
Expand Down Expand Up @@ -1592,10 +1589,10 @@ describe("HubConnection", () => {
expect(observer.itemsReceived).toEqual([1]);

// Close message sent asynchronously so we need to wait
await delayUntil(1000, () => connection.sentData.length === 3);
await delayUntil(1000, () => connection.sentData.length === 4);
// Verify the cancel is sent (+ handshake)
expect(connection.sentData.length).toBe(3);
expect(JSON.parse(connection.sentData[2])).toEqual({
expect(connection.sentData.length).toBe(4);
expect(JSON.parse(connection.sentData[3])).toEqual({
invocationId: connection.lastInvocationId,
type: MessageType.CancelInvocation,
});
Expand Down Expand Up @@ -1830,7 +1827,9 @@ class TestProtocol implements IHubProtocol {
}

public writeMessage(message: HubMessage): any {

if (message.type === 6) {
return "{\"type\": 6}" + TextMessageFormat.RecordSeparator;
}
}
}

Expand Down