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

View File

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

View File

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

View File

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