Skip to content

Commit 214e86b

Browse files
committed
Set transport handlers earlier in Typescript client
1 parent b9823ed commit 214e86b

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/SignalR/clients/ts/signalr/src/HttpConnection.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,6 @@ export class HttpConnection implements IConnection {
281281
this.features.inherentKeepAlive = true;
282282
}
283283

284-
this.transport!.onreceive = this.onreceive;
285-
this.transport!.onclose = (e) => this.stopConnection(e);
286-
287284
if (this.connectionState === ConnectionState.Connecting) {
288285
// Ensure the connection transitions to the connected state prior to completing this.startInternalPromise.
289286
// start() will handle the case when stop was called and startInternal exits still in the disconnecting state.
@@ -344,6 +341,8 @@ export class HttpConnection implements IConnection {
344341
if (this.isITransport(requestedTransport)) {
345342
this.logger.log(LogLevel.Debug, "Connection was provided an instance of ITransport, using that directly.");
346343
this.transport = requestedTransport;
344+
this.transport.onreceive = this.onreceive;
345+
this.transport.onclose = (e) => this.stopConnection(e);
347346
await this.transport.connect(connectUrl, requestedTransferFormat);
348347

349348
return;
@@ -367,6 +366,8 @@ export class HttpConnection implements IConnection {
367366
connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);
368367
}
369368
try {
369+
this.transport!.onreceive = this.onreceive;
370+
this.transport!.onclose = (e) => this.stopConnection(e);
370371
await this.transport!.connect(connectUrl, requestedTransferFormat);
371372
return;
372373
} catch (ex) {

src/SignalR/clients/ts/signalr/tests/HttpConnection.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,42 @@ describe("HttpConnection", () => {
795795
});
796796
});
797797

798+
it("transport handlers set before start", async () => {
799+
await VerifyLogger.run(async (logger) => {
800+
const availableTransport = { transport: "LongPolling", transferFormats: ["Text"] };
801+
let handlersSet = false;
802+
803+
let httpClientGetCount = 0;
804+
const options: IHttpConnectionOptions = {
805+
...commonOptions,
806+
httpClient: new TestHttpClient()
807+
.on("POST", () => ({ connectionId: "42", availableTransports: [availableTransport] }))
808+
.on("GET", () => {
809+
httpClientGetCount++;
810+
if (httpClientGetCount === 1) {
811+
if ((connection as any).transport.onreceive && (connection as any).transport.onclose) {
812+
handlersSet = true;
813+
}
814+
// First long polling request must succeed so start completes
815+
return "";
816+
}
817+
})
818+
.on("DELETE", () => new HttpResponse(202)),
819+
logger,
820+
} as IHttpConnectionOptions;
821+
822+
const connection = new HttpConnection("http://tempuri.org", options);
823+
connection.onreceive = () => null;
824+
try {
825+
await connection.start(TransferFormat.Text);
826+
} finally {
827+
await connection.stop();
828+
}
829+
830+
expect(handlersSet).toBe(true);
831+
});
832+
});
833+
798834
describe(".constructor", () => {
799835
it("throws if no Url is provided", async () => {
800836
// Force TypeScript to let us call the constructor incorrectly :)

0 commit comments

Comments
 (0)