From 58e7c1b1fe003cefc43dd6ff00b3c6ce89db3d4a Mon Sep 17 00:00:00 2001 From: moozzyk Date: Mon, 17 Apr 2017 15:08:43 -0700 Subject: [PATCH] Allowing passing ITransport to start --- .../HubConnection.spec.ts | 6 ++--- .../Connection.ts | 24 ++++++++++++------- .../IConnection.ts | 4 ++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts index 8bc23fc3e0..c3eae1fb51 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts @@ -1,12 +1,12 @@ import { IConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/IConnection" import { HubConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/HubConnection" import { DataReceived, ConnectionClosed } from "../Microsoft.AspNetCore.SignalR.Client.TS/Common" -import { TransportType } from "../Microsoft.AspNetCore.SignalR.Client.TS/Transports" +import { TransportType, ITransport } from "../Microsoft.AspNetCore.SignalR.Client.TS/Transports" describe("HubConnection", () => { it("completes pending invocations when stopped", async (done) => { let connection: IConnection = { - start(transportType: TransportType): Promise { + start(transportType: TransportType | ITransport): Promise { return Promise.resolve(); }, @@ -40,7 +40,7 @@ describe("HubConnection", () => { it("completes pending invocations when connection is lost", async (done) => { let connection: IConnection = { - start(transportType: TransportType): Promise { + start(transportType: TransportType | ITransport): Promise { return Promise.resolve(); }, diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts index 69de36fffb..26489544d2 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts @@ -27,18 +27,18 @@ export class Connection implements IConnection { this.connectionState = ConnectionState.Initial; } - async start(transportType: TransportType = TransportType.WebSockets): Promise { + async start(transport: TransportType | ITransport = TransportType.WebSockets): Promise { if (this.connectionState != ConnectionState.Initial) { return Promise.reject(new Error("Cannot start a connection that is not in the 'Initial' state.")); } this.connectionState = ConnectionState.Connecting; - this.startPromise = this.startInternal(transportType); + this.startPromise = this.startInternal(transport); return this.startPromise; } - private async startInternal(transportType: TransportType): Promise { + private async startInternal(transportType: TransportType | ITransport): Promise { try { this.connectionId = await this.httpClient.get(`${this.url}/negotiate?${this.queryString}`); @@ -57,7 +57,7 @@ export class Connection implements IConnection { // the state if the connection is already marked as Disconnected this.changeState(ConnectionState.Connecting, ConnectionState.Connected); } - catch(e) { + catch (e) { console.log("Failed to start the connection. " + e) this.connectionState = ConnectionState.Disconnected; this.transport = null; @@ -65,20 +65,28 @@ export class Connection implements IConnection { }; } - private createTransport(transportType: TransportType): ITransport { - if (transportType === TransportType.WebSockets) { + private createTransport(transport: TransportType | ITransport): ITransport { + if (transport === TransportType.WebSockets) { return new WebSocketTransport(); } - if (transportType === TransportType.ServerSentEvents) { + if (transport === TransportType.ServerSentEvents) { return new ServerSentEventsTransport(this.httpClient); } - if (transportType === TransportType.LongPolling) { + if (transport === TransportType.LongPolling) { return new LongPollingTransport(this.httpClient); } + if (this.isITransport(transport)) { + return transport; + } + throw new Error("No valid transports requested."); } + private isITransport(transport: any): transport is ITransport { + return "connect" in transport; + } + private changeState(from: ConnectionState, to: ConnectionState): Boolean { if (this.connectionState == from) { this.connectionState = to; diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts index 3ac44fe4f0..3c230def17 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts @@ -1,8 +1,8 @@ import { DataReceived, ConnectionClosed } from "./Common" -import { TransportType } from "./Transports" +import { TransportType, ITransport } from "./Transports" export interface IConnection { - start(transportType: TransportType): Promise; + start(transportType: TransportType | ITransport): Promise; send(data: any): Promise; stop(): void;