Making HttpClient configurable and reusable
This commit is contained in:
parent
bd19022c4c
commit
a9c7e14da0
|
|
@ -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.");
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
export class HttpClient {
|
||||
export interface IHttpClient {
|
||||
get(url: string): Promise<string>;
|
||||
post(url: string, content: string): Promise<string>;
|
||||
}
|
||||
|
||||
export class HttpClient implements IHttpClient {
|
||||
get(url: string): Promise<string> {
|
||||
return this.xhr("GET", url);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
import {IHttpClient} from "./HttpClient"
|
||||
|
||||
export interface ISignalROptions {
|
||||
httpClient?: IHttpClient;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { HttpClient } from "./HttpClient"
|
||||
import { IHttpClient } from "./HttpClient"
|
||||
|
||||
export interface ITransport {
|
||||
connect(url: string, queryString: string): Promise<void>;
|
||||
|
|
@ -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<void> {
|
||||
if (typeof (EventSource) === "undefined") {
|
||||
|
|
@ -113,7 +118,7 @@ export class ServerSentEventsTransport implements ITransport {
|
|||
}
|
||||
|
||||
async send(data: any): Promise<void> {
|
||||
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<void> {
|
||||
this.url = url;
|
||||
this.queryString = queryString;
|
||||
|
|
@ -190,7 +200,7 @@ export class LongPollingTransport implements ITransport {
|
|||
}
|
||||
|
||||
async send(data: any): Promise<void> {
|
||||
await new HttpClient().post(this.url + "/send?" + this.queryString, data);
|
||||
await this.httpClient.post(this.url + "/send?" + this.queryString, data);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
|
|
|
|||
Loading…
Reference in New Issue