diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts index 39d89f4867..e730ccf392 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts @@ -1,5 +1,6 @@ import { ITransport, WebSocketTransport, ServerSentEventsTransport, LongPollingTransport } from "./Transports" -import { HttpClient } from "./HttpClient" +import { IHttpClient, HttpClient } from "./HttpClient" +import { ISignalROptions } from "./ISignalROptions" enum ConnectionState { Disconnected, @@ -12,13 +13,15 @@ export class Connection { private url: string; private queryString: string; private connectionId: string; + private httpClient: IHttpClient; private transport: ITransport; private dataReceivedCallback: DataReceived = (data: any) => { }; private connectionClosedCallback: ConnectionClosed = (error?: any) => { }; - constructor(url: string, queryString: string = "") { + constructor(url: string, queryString: string = "", options: ISignalROptions = {}) { this.url = url; this.queryString = queryString; + this.httpClient = options.httpClient || new HttpClient(); this.connectionState = ConnectionState.Disconnected; } @@ -29,10 +32,10 @@ export class Connection { this.transport = this.createTransport(transportName); this.transport.onDataReceived = this.dataReceivedCallback; - this.transport.onError = e => this.stopConnection(); + this.transport.onError = e => this.stopConnection(e); try { - this.connectionId = await new HttpClient().get(`${this.url}/getid?${this.queryString}`); + this.connectionId = await this.httpClient.get(`${this.url}/getid?${this.queryString}`); this.queryString = `id=${this.connectionId}`; await this.transport.connect(this.url, this.queryString); this.connectionState = ConnectionState.Connected; @@ -50,10 +53,10 @@ export class Connection { return new WebSocketTransport(); } if (transportName === 'serverSentEvents') { - return new ServerSentEventsTransport(); + return new ServerSentEventsTransport(this.httpClient); } if (transportName === 'longPolling') { - return new LongPollingTransport(); + return new LongPollingTransport(this.httpClient); } throw new Error("No valid transports requested."); diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts index dbb7cc6a85..f94d719ffe 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts @@ -1,4 +1,9 @@ -export class HttpClient { +export interface IHttpClient { + get(url: string): Promise; + post(url: string, content: string): Promise; +} + +export class HttpClient implements IHttpClient { get(url: string): Promise { return this.xhr("GET", url); } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/ISignalROptions.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/ISignalROptions.ts new file mode 100644 index 0000000000..1c696ca23c --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/ISignalROptions.ts @@ -0,0 +1,5 @@ +import {IHttpClient} from "./HttpClient" + +export interface ISignalROptions { + httpClient?: IHttpClient; +} diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts index 16893a8ed1..367d68dc94 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts @@ -1,4 +1,4 @@ -import { HttpClient } from "./HttpClient" +import { IHttpClient } from "./HttpClient" export interface ITransport { connect(url: string, queryString: string): Promise; @@ -71,6 +71,11 @@ export class ServerSentEventsTransport implements ITransport { private eventSource: EventSource; private url: string; private queryString: string; + private httpClient: IHttpClient; + + constructor(httpClient :IHttpClient) { + this.httpClient = httpClient; + } connect(url: string, queryString: string): Promise { if (typeof (EventSource) === "undefined") { @@ -113,7 +118,7 @@ export class ServerSentEventsTransport implements ITransport { } async send(data: any): Promise { - await new HttpClient().post(this.url + "/send?" + this.queryString, data); + await this.httpClient.post(this.url + "/send?" + this.queryString, data); } stop(): void { @@ -130,9 +135,14 @@ export class ServerSentEventsTransport implements ITransport { export class LongPollingTransport implements ITransport { private url: string; private queryString: string; + private httpClient: IHttpClient; private pollXhr: XMLHttpRequest; private shouldPoll: boolean; + constructor(httpClient :IHttpClient) { + this.httpClient = httpClient; + } + connect(url: string, queryString: string): Promise { this.url = url; this.queryString = queryString; @@ -190,7 +200,7 @@ export class LongPollingTransport implements ITransport { } async send(data: any): Promise { - await new HttpClient().post(this.url + "/send?" + this.queryString, data); + await this.httpClient.post(this.url + "/send?" + this.queryString, data); } stop(): void {