Removing unneeded queryString from TS client

This commit is contained in:
Pawel Kadluczka 2017-06-06 13:55:15 -07:00 committed by Pawel Kadluczka
parent d8583535c8
commit 028bd68bf5
4 changed files with 24 additions and 39 deletions

View File

@ -18,7 +18,7 @@ describe("Connection", () => {
} }
} as ISignalROptions; } as ISignalROptions;
let connection = new Connection("http://tempuri.org", undefined, options); let connection = new Connection("http://tempuri.org", options);
try { try {
await connection.start(); await connection.start();
@ -53,7 +53,7 @@ describe("Connection", () => {
} }
} as ISignalROptions; } as ISignalROptions;
let connection = new Connection("http://tempuri.org", undefined, options); let connection = new Connection("http://tempuri.org", options);
try { try {
await connection.start(); await connection.start();
@ -76,7 +76,7 @@ describe("Connection", () => {
} }
} as ISignalROptions; } as ISignalROptions;
let connection = new Connection("http://tempuri.org", undefined, options); let connection = new Connection("http://tempuri.org", options);
try { try {
// start will fail and transition the connection to the Disconnected state // start will fail and transition the connection to the Disconnected state
@ -111,7 +111,7 @@ describe("Connection", () => {
} }
} as ISignalROptions; } as ISignalROptions;
var connection = new Connection("http://tempuri.org", undefined, options); var connection = new Connection("http://tempuri.org", options);
try { try {
await connection.start(); await connection.start();
@ -141,10 +141,10 @@ describe("Connection", () => {
} }
} as ISignalROptions; } as ISignalROptions;
let connectQueryString: string; let connectUrl: string;
let fakeTransport: ITransport = { let fakeTransport: ITransport = {
connect(url: string, queryString: string): Promise<void> { connect(url: string): Promise<void> {
connectQueryString = queryString; connectUrl = url;
return Promise.reject(""); return Promise.reject("");
}, },
send(data: any): Promise<void> { send(data: any): Promise<void> {
@ -155,7 +155,7 @@ describe("Connection", () => {
onClosed: undefined onClosed: undefined
} }
var connection = new Connection("http://tempuri.org", "q=myData", options); var connection = new Connection("http://tempuri.org?q=myData", options);
try { try {
await connection.start(fakeTransport); await connection.start(fakeTransport);
@ -165,7 +165,7 @@ describe("Connection", () => {
catch (e) { catch (e) {
} }
expect(connectQueryString).toBe("q=myData&id=42"); expect(connectUrl).toBe("http://tempuri.org?q=myData&id=42");
done(); done();
}); });
}); });

View File

@ -14,15 +14,13 @@ enum ConnectionState {
export class Connection implements IConnection { export class Connection implements IConnection {
private connectionState: ConnectionState; private connectionState: ConnectionState;
private url: string; private url: string;
private queryString: string;
private connectionId: string; private connectionId: string;
private httpClient: IHttpClient; private httpClient: IHttpClient;
private transport: ITransport; private transport: ITransport;
private startPromise: Promise<void>; private startPromise: Promise<void>;
constructor(url: string, queryString: string = "", options: ISignalROptions = {}) { constructor(url: string, options: ISignalROptions = {}) {
this.url = url; this.url = url;
this.queryString = queryString || "";
this.httpClient = options.httpClient || new HttpClient(); this.httpClient = options.httpClient || new HttpClient();
this.connectionState = ConnectionState.Initial; this.connectionState = ConnectionState.Initial;
} }
@ -40,23 +38,19 @@ export class Connection implements IConnection {
private async startInternal(transportType: TransportType | ITransport): Promise<void> { private async startInternal(transportType: TransportType | ITransport): Promise<void> {
try { try {
var negotiateUrl = this.url + (this.queryString ? "?" + this.queryString : ""); this.connectionId = await this.httpClient.options(this.url);
this.connectionId = await this.httpClient.options(negotiateUrl);
// the user tries to stop the the connection when it is being started // the user tries to stop the the connection when it is being started
if (this.connectionState == ConnectionState.Disconnected) { if (this.connectionState == ConnectionState.Disconnected) {
return; return;
} }
if (this.queryString) { this.url += (this.url.indexOf("?") == -1 ? "?" : "&") + `id=${this.connectionId}`;
this.queryString += "&";
}
this.queryString += `id=${this.connectionId}`;
this.transport = this.createTransport(transportType); this.transport = this.createTransport(transportType);
this.transport.onDataReceived = this.onDataReceived; this.transport.onDataReceived = this.onDataReceived;
this.transport.onClosed = e => this.stopConnection(true, e); this.transport.onClosed = e => this.stopConnection(true, e);
await this.transport.connect(this.url, this.queryString); await this.transport.connect(this.url);
// only change the state if we were connecting to not overwrite // only change the state if we were connecting to not overwrite
// 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);

View File

@ -8,7 +8,7 @@ export enum TransportType {
} }
export interface ITransport { export interface ITransport {
connect(url: string, queryString: string): Promise<void>; connect(url: string): Promise<void>;
send(data: any): Promise<void>; send(data: any): Promise<void>;
stop(): void; stop(): void;
onDataReceived: DataReceived; onDataReceived: DataReceived;
@ -21,12 +21,11 @@ export class WebSocketTransport implements ITransport {
connect(url: string, queryString: string = ""): Promise<void> { connect(url: string, queryString: string = ""): Promise<void> {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
url = url.replace(/^http/, "ws"); url = url.replace(/^http/, "ws");
let connectUrl = url + (queryString ? "?" + queryString : "");
let webSocket = new WebSocket(connectUrl); let webSocket = new WebSocket(url);
webSocket.onopen = (event: Event) => { webSocket.onopen = (event: Event) => {
console.log(`WebSocket connected to ${connectUrl}`); console.log(`WebSocket connected to ${url}`);
this.webSocket = webSocket; this.webSocket = webSocket;
resolve(); resolve();
}; };
@ -80,24 +79,20 @@ export class ServerSentEventsTransport implements ITransport {
private eventSource: EventSource; private eventSource: EventSource;
private url: string; private url: string;
private queryString: string; private queryString: string;
private fullUrl: string;
private httpClient: IHttpClient; private httpClient: IHttpClient;
constructor(httpClient: IHttpClient) { constructor(httpClient: IHttpClient) {
this.httpClient = httpClient; this.httpClient = httpClient;
} }
connect(url: string, queryString: string): Promise<void> { connect(url: string): Promise<void> {
if (typeof (EventSource) === "undefined") { if (typeof (EventSource) === "undefined") {
Promise.reject("EventSource not supported by the browser.") Promise.reject("EventSource not supported by the browser.")
} }
this.queryString = queryString;
this.url = url; this.url = url;
this.fullUrl = url + (queryString ? "?" + queryString : "");
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
let eventSource = new EventSource(this.fullUrl); let eventSource = new EventSource(this.url);
try { try {
eventSource.onmessage = (e: MessageEvent) => { eventSource.onmessage = (e: MessageEvent) => {
@ -124,7 +119,7 @@ export class ServerSentEventsTransport implements ITransport {
} }
eventSource.onopen = () => { eventSource.onopen = () => {
console.log(`SSE connected to ${this.fullUrl}`); console.log(`SSE connected to ${this.url}`);
this.eventSource = eventSource; this.eventSource = eventSource;
resolve(); resolve();
} }
@ -136,7 +131,7 @@ export class ServerSentEventsTransport implements ITransport {
} }
async send(data: any): Promise<void> { async send(data: any): Promise<void> {
return send(this.httpClient, this.fullUrl, data); return send(this.httpClient, this.url, data);
} }
stop(): void { stop(): void {
@ -152,8 +147,6 @@ export class ServerSentEventsTransport implements ITransport {
export class LongPollingTransport implements ITransport { export class LongPollingTransport implements ITransport {
private url: string; private url: string;
private queryString: string;
private fullUrl: string;
private httpClient: IHttpClient; private httpClient: IHttpClient;
private pollXhr: XMLHttpRequest; private pollXhr: XMLHttpRequest;
private shouldPoll: boolean; private shouldPoll: boolean;
@ -162,12 +155,10 @@ export class LongPollingTransport implements ITransport {
this.httpClient = httpClient; this.httpClient = httpClient;
} }
connect(url: string, queryString: string): Promise<void> { connect(url: string): Promise<void> {
this.url = url; this.url = url;
this.queryString = queryString;
this.shouldPoll = true; this.shouldPoll = true;
this.fullUrl = url + (queryString ? "?" + queryString : ""); this.poll(this.url);
this.poll(this.fullUrl);
return Promise.resolve(); return Promise.resolve();
} }
@ -224,7 +215,7 @@ export class LongPollingTransport implements ITransport {
} }
async send(data: any): Promise<void> { async send(data: any): Promise<void> {
return send(this.httpClient, this.fullUrl, data); return send(this.httpClient, this.url, data);
} }
stop(): void { stop(): void {

View File

@ -3,7 +3,7 @@
<Import Project="..\..\build\common.props" /> <Import Project="..\..\build\common.props" />
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>