Fixing LongPolling transport

This commit is contained in:
moozzyk 2016-11-01 14:03:39 -07:00
parent ce17f0d19a
commit 61c527f23c
3 changed files with 13 additions and 23 deletions

View File

@ -65,7 +65,7 @@ class Connection {
transports.push(new ServerSentEventsTransport());
}
if (transportName === 'longPolling') {
transports.push(new LongPollingTransport(null));
transports.push(new LongPollingTransport());
}
});
@ -100,14 +100,6 @@ class Connection {
return this.transport.send(data);
}
set dataReceived(callback: DataReceived) {
this.dataReceivedCallback = callback;
}
set onError(callback: ErrorHandler) {
this.errorHandler = callback;
}
stop(): void {
if (this.connectionState != ConnectionState.Connected) {
throw new Error("Cannot stop the connection if is not in the 'Connected' State");
@ -116,4 +108,12 @@ class Connection {
this.transport.stop();
this.connectionState = ConnectionState.Disconnected;
}
set dataReceived(callback: DataReceived) {
this.dataReceivedCallback = callback;
}
set onError(callback: ErrorHandler) {
this.errorHandler = callback;
}
}

View File

@ -1,20 +1,11 @@
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;
}
connect(url: string, queryString: string): Promise<void> {
this.queryString = queryString || "";
this.url = url || "";
this.url = url;
this.queryString = queryString;
this.pollXhr = new XMLHttpRequest();
// TODO: resolve promise on open sending? + reject on error
this.poll(url + "/poll?" + this.queryString)
@ -27,7 +18,7 @@ class LongPollingTransport implements ITransport {
this.pollXhr.send();
this.pollXhr.onload = () => {
if (this.pollXhr.status >= 200 && this.pollXhr.status < 300) {
this.receiveCallback(this.pollXhr.response);
this.onDataReceived(this.pollXhr.response);
this.poll(url);
}
else {

View File

@ -1,12 +1,11 @@
// TODO: need EvenSource typings
class ServerSentEventsTransport implements ITransport {
private receiveCallback: (data: string) => void;
private eventSource: EventSource;
private url: string;
private queryString: string;
connect(url: string, queryString: string = ""): Promise<void> {
connect(url: string, queryString: string): Promise<void> {
if (typeof (EventSource) === "undefined") {
Promise.reject("EventSource not supported by the browser.")
}