Fixing LongPolling transport
This commit is contained in:
parent
ce17f0d19a
commit
61c527f23c
|
|
@ -65,7 +65,7 @@ class Connection {
|
||||||
transports.push(new ServerSentEventsTransport());
|
transports.push(new ServerSentEventsTransport());
|
||||||
}
|
}
|
||||||
if (transportName === 'longPolling') {
|
if (transportName === 'longPolling') {
|
||||||
transports.push(new LongPollingTransport(null));
|
transports.push(new LongPollingTransport());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -100,14 +100,6 @@ class Connection {
|
||||||
return this.transport.send(data);
|
return this.transport.send(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
set dataReceived(callback: DataReceived) {
|
|
||||||
this.dataReceivedCallback = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
set onError(callback: ErrorHandler) {
|
|
||||||
this.errorHandler = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
stop(): void {
|
stop(): void {
|
||||||
if (this.connectionState != ConnectionState.Connected) {
|
if (this.connectionState != ConnectionState.Connected) {
|
||||||
throw new Error("Cannot stop the connection if is not in the 'Connected' State");
|
throw new Error("Cannot stop the connection if is not in the 'Connected' State");
|
||||||
|
|
@ -116,4 +108,12 @@ class Connection {
|
||||||
this.transport.stop();
|
this.transport.stop();
|
||||||
this.connectionState = ConnectionState.Disconnected;
|
this.connectionState = ConnectionState.Disconnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set dataReceived(callback: DataReceived) {
|
||||||
|
this.dataReceivedCallback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
set onError(callback: ErrorHandler) {
|
||||||
|
this.errorHandler = callback;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,20 +1,11 @@
|
||||||
class LongPollingTransport implements ITransport {
|
class LongPollingTransport implements ITransport {
|
||||||
|
|
||||||
private receiveCallback: (data: string) => void;
|
|
||||||
private url: string;
|
private url: string;
|
||||||
private queryString: string;
|
private queryString: string;
|
||||||
private pollXhr: XMLHttpRequest;
|
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> {
|
connect(url: string, queryString: string): Promise<void> {
|
||||||
this.queryString = queryString || "";
|
this.url = url;
|
||||||
this.url = url || "";
|
this.queryString = queryString;
|
||||||
|
|
||||||
this.pollXhr = new XMLHttpRequest();
|
this.pollXhr = new XMLHttpRequest();
|
||||||
// TODO: resolve promise on open sending? + reject on error
|
// TODO: resolve promise on open sending? + reject on error
|
||||||
this.poll(url + "/poll?" + this.queryString)
|
this.poll(url + "/poll?" + this.queryString)
|
||||||
|
|
@ -27,7 +18,7 @@ class LongPollingTransport implements ITransport {
|
||||||
this.pollXhr.send();
|
this.pollXhr.send();
|
||||||
this.pollXhr.onload = () => {
|
this.pollXhr.onload = () => {
|
||||||
if (this.pollXhr.status >= 200 && this.pollXhr.status < 300) {
|
if (this.pollXhr.status >= 200 && this.pollXhr.status < 300) {
|
||||||
this.receiveCallback(this.pollXhr.response);
|
this.onDataReceived(this.pollXhr.response);
|
||||||
this.poll(url);
|
this.poll(url);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
// TODO: need EvenSource typings
|
// TODO: need EvenSource typings
|
||||||
|
|
||||||
class ServerSentEventsTransport implements ITransport {
|
class ServerSentEventsTransport implements ITransport {
|
||||||
private receiveCallback: (data: string) => void;
|
|
||||||
private eventSource: EventSource;
|
private eventSource: EventSource;
|
||||||
private url: string;
|
private url: string;
|
||||||
private queryString: string;
|
private queryString: string;
|
||||||
|
|
||||||
connect(url: string, queryString: string = ""): Promise<void> {
|
connect(url: string, queryString: 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.")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue