From 56631987331c25ac236159322b583cbeee655e19 Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Tue, 25 Oct 2016 12:22:28 -0700 Subject: [PATCH] WIP --- .../HttpClient.ts | 6 +- .../LongPollingTransport.ts | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/Microsoft.AspNetCore.SignalR.Client.TS/LongPollingTransport.ts diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts index 96f18aeb86..cf7ab4883d 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/HttpClient.ts @@ -3,15 +3,15 @@ class HttpClient { return this.xhr("GET", url); } - post(url: string): Promise { + post(url: string, content: string): Promise { return this.xhr("POST", url); } - private xhr(method: string, url: string): Promise { + private xhr(method: string, url: string, content?: string): Promise { return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest(); xhr.open(method, url, true); - xhr.send(); + xhr.send(content); xhr.onload = () => { if (xhr.status >= 200 && xhr.status < 300) { resolve(xhr.response); diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/LongPollingTransport.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/LongPollingTransport.ts new file mode 100644 index 0000000000..5ecf60cbc5 --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/LongPollingTransport.ts @@ -0,0 +1,56 @@ +class LongPollingTransport implements ITransport { + + private receiveCallback: (data: string) => void; + private url: string; + private queryString: string; + private pollXhr: XMLHttpRequest; + + // TODO: make the callback a named type + // TODO: string won't work for binary formats + constructor(receiveCallback: (data: string) => void) { + this.receiveCallback = receiveCallback; + this.pollXhr = new XMLHttpRequest(); + } + + connect(url: string, queryString: string): Promise { + + return Promise.resolve(); + } + + private poll(): void { + this.pollXhr.open("GET", , true); + this.pollXhr.send(); + this.pollXhr.onload = () => { + if (this.pollXhr.status >= 200 && this.pollXhr.status < 300) { + this.receiveCallback(this.pollXhr.response); + this.poll(); + } + else { + //TODO: handle error + /* + { + status: xhr.status, + statusText: xhr.statusText + }; + }*/ + }; + + this.pollXhr.onerror = () => { + /* + reject({ + status: xhr.status, + statusText: xhr.statusText + });*/ + //TODO: handle error + }; + }; + } + + send(data: string): Promise { + return new HttpClient().post(this.url + "/poll/send?" + this.queryString, data); + } + + stop(): void { + this.pollXhr.abort(); + } +} \ No newline at end of file