Allowing passing ITransport to start
This commit is contained in:
parent
b45e20acec
commit
58e7c1b1fe
|
|
@ -1,12 +1,12 @@
|
||||||
import { IConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/IConnection"
|
import { IConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/IConnection"
|
||||||
import { HubConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/HubConnection"
|
import { HubConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/HubConnection"
|
||||||
import { DataReceived, ConnectionClosed } from "../Microsoft.AspNetCore.SignalR.Client.TS/Common"
|
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", () => {
|
describe("HubConnection", () => {
|
||||||
it("completes pending invocations when stopped", async (done) => {
|
it("completes pending invocations when stopped", async (done) => {
|
||||||
let connection: IConnection = {
|
let connection: IConnection = {
|
||||||
start(transportType: TransportType): Promise<void> {
|
start(transportType: TransportType | ITransport): Promise<void> {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -40,7 +40,7 @@ describe("HubConnection", () => {
|
||||||
|
|
||||||
it("completes pending invocations when connection is lost", async (done) => {
|
it("completes pending invocations when connection is lost", async (done) => {
|
||||||
let connection: IConnection = {
|
let connection: IConnection = {
|
||||||
start(transportType: TransportType): Promise<void> {
|
start(transportType: TransportType | ITransport): Promise<void> {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,18 +27,18 @@ export class Connection implements IConnection {
|
||||||
this.connectionState = ConnectionState.Initial;
|
this.connectionState = ConnectionState.Initial;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(transportType: TransportType = TransportType.WebSockets): Promise<void> {
|
async start(transport: TransportType | ITransport = TransportType.WebSockets): Promise<void> {
|
||||||
if (this.connectionState != ConnectionState.Initial) {
|
if (this.connectionState != ConnectionState.Initial) {
|
||||||
return Promise.reject(new Error("Cannot start a connection that is not in the 'Initial' state."));
|
return Promise.reject(new Error("Cannot start a connection that is not in the 'Initial' state."));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.connectionState = ConnectionState.Connecting;
|
this.connectionState = ConnectionState.Connecting;
|
||||||
|
|
||||||
this.startPromise = this.startInternal(transportType);
|
this.startPromise = this.startInternal(transport);
|
||||||
return this.startPromise;
|
return this.startPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async startInternal(transportType: TransportType): Promise<void> {
|
private async startInternal(transportType: TransportType | ITransport): Promise<void> {
|
||||||
try {
|
try {
|
||||||
this.connectionId = await this.httpClient.get(`${this.url}/negotiate?${this.queryString}`);
|
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
|
// the state if the connection is already marked as Disconnected
|
||||||
this.changeState(ConnectionState.Connecting, ConnectionState.Connected);
|
this.changeState(ConnectionState.Connecting, ConnectionState.Connected);
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch (e) {
|
||||||
console.log("Failed to start the connection. " + e)
|
console.log("Failed to start the connection. " + e)
|
||||||
this.connectionState = ConnectionState.Disconnected;
|
this.connectionState = ConnectionState.Disconnected;
|
||||||
this.transport = null;
|
this.transport = null;
|
||||||
|
|
@ -65,20 +65,28 @@ export class Connection implements IConnection {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private createTransport(transportType: TransportType): ITransport {
|
private createTransport(transport: TransportType | ITransport): ITransport {
|
||||||
if (transportType === TransportType.WebSockets) {
|
if (transport === TransportType.WebSockets) {
|
||||||
return new WebSocketTransport();
|
return new WebSocketTransport();
|
||||||
}
|
}
|
||||||
if (transportType === TransportType.ServerSentEvents) {
|
if (transport === TransportType.ServerSentEvents) {
|
||||||
return new ServerSentEventsTransport(this.httpClient);
|
return new ServerSentEventsTransport(this.httpClient);
|
||||||
}
|
}
|
||||||
if (transportType === TransportType.LongPolling) {
|
if (transport === TransportType.LongPolling) {
|
||||||
return new LongPollingTransport(this.httpClient);
|
return new LongPollingTransport(this.httpClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.isITransport(transport)) {
|
||||||
|
return transport;
|
||||||
|
}
|
||||||
|
|
||||||
throw new Error("No valid transports requested.");
|
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 {
|
private changeState(from: ConnectionState, to: ConnectionState): Boolean {
|
||||||
if (this.connectionState == from) {
|
if (this.connectionState == from) {
|
||||||
this.connectionState = to;
|
this.connectionState = to;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { DataReceived, ConnectionClosed } from "./Common"
|
import { DataReceived, ConnectionClosed } from "./Common"
|
||||||
import { TransportType } from "./Transports"
|
import { TransportType, ITransport } from "./Transports"
|
||||||
|
|
||||||
export interface IConnection {
|
export interface IConnection {
|
||||||
start(transportType: TransportType): Promise<void>;
|
start(transportType: TransportType | ITransport): Promise<void>;
|
||||||
send(data: any): Promise<void>;
|
send(data: any): Promise<void>;
|
||||||
stop(): void;
|
stop(): void;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue