Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Making HttpClient configurable and reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
moozzyk committed Jan 11, 2017
1 parent bd19022 commit a9c7e14
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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.");
Expand Down
7 changes: 6 additions & 1 deletion src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.AspNetCore.SignalR.Client.TS/ISignalROptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {IHttpClient} from "./HttpClient"

export interface ISignalROptions {
httpClient?: IHttpClient;
}
16 changes: 13 additions & 3 deletions src/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClient } from "./HttpClient"
import { IHttpClient } from "./HttpClient"

export interface ITransport {
connect(url: string, queryString: string): Promise<void>;
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a9c7e14

Please sign in to comment.