HttpTransport class
Transport mechanism using ajax polling used by createHttpConnection.
class HttpTransport extends Transport {
/**
* RequestFactory is a function like HttpRequest.request() that returns
* [Future<HttpRequest>].
*/
final _sendHttpRequest;
/**
* The URL where to perform requests.
*/
final String _url;
/**
* Indicates whether a [HttpRequest] is currently on the way.
*/
bool _isRunning = false;
/**
* Time interval between response to a request is received and next request
* is sent.
*/
Duration _delayBetweenRequests;
Timer _timer;
bool _connected = true;
void _disconnect() {
_connected = false;
_disconnectConnection();
}
void _reconnect() {
_connected = true;
_reconnectConnection();
}
HttpTransport(this._sendHttpRequest, this._url, this._delayBetweenRequests, [this._timeout = null]);
/**
* Seconds after which request is declared as timed-out. Optional parameter.
* Use only with HttpRequest factories which support it. (Like the one in http_request.dart)
*/
int _timeout;
int get timeout => _timeout;
setHandlers(prepareRequest, handleResponse, handleError, [handleDisconnect = null, handleReconnect = null]) {
super.setHandlers(prepareRequest, handleResponse, handleError, handleDisconnect, handleReconnect);
_timer = new Timer.periodic(this._delayBetweenRequests, (_) => _performRequest());
}
/**
* Notifies [HttpTransport] instance that there are some requests to be sent
* and attempts to send them immediately. If a HttpRequest is already running,
* the new requests will be sent in next "iteration" (after response is
* received + time interval _delayBetweenRequests passes).
*/
markDirty() {}
/**
* Marks timer as disposed, which prevents him from future sending of http
* requests.
*/
dispose() {
if(_timer != null) _timer.cancel();
}
bool _shouldSendHttpRequest() => !_isRunning && _connected;
void _openRequest() {
_isRunning = true;
}
void _closeRequest() {
_isRunning = false;
}
Future _buildRequest(data) {
if (null == _timeout) {
return _sendHttpRequest(
_url,
method: 'POST',
requestHeaders: {'Content-Type': 'application/json'},
sendData: JSON.encode(data)
);
} else {
return _sendHttpRequest(
_url,
method: 'POST',
requestHeaders: {'Content-Type': 'application/json'},
sendData: JSON.encode(data),
timeout: _timeout
);
}
}
/**
* Begins performing HttpRequest. Is not launched if another request is
* already running ([_isRunning] is true) or the request Queue is empty,
* ([_isDirty] is false). Sets [_isRunning] as true for the time this request
* is running and hooks up another request after this one with a delay of
* [_delayBetweenRequests].
*/
void _performRequest() {
if (_connected) _sendDataRequest();
else _sendPingRequest();
}
void _sendDataRequest() {
if (!_shouldSendHttpRequest()) {
return;
}
var data = _prepareRequest();
if (data.isEmpty) return;
_openRequest();
_buildRequest(data).then((xhr) {
_handleResponse(JSON.decode(xhr.responseText));
_closeRequest();
}).catchError((e, s) {
if (e is ConnectionError) {
_handleError(e);
_disconnect();
} else {
logger.shout("error", e, s);
_handleError(new FailedRequestException());
}
_closeRequest();
});
}
void _sendPingRequest() {
_openRequest();
_buildRequest([new PackedRequest(0, new ClientRequest('ping', 'ping'))]).then((xhr) {
_reconnect();
_closeRequest();
}).catchError((e) {
if (e is ConnectionError) {
} else {
_reconnect();
}
_closeRequest();
});
}
}
Extends
Transport > HttpTransport
Constructors
Methods
dynamic dispose() #
Marks timer as disposed, which prevents him from future sending of http requests.
dispose() {
if(_timer != null) _timer.cancel();
}
dynamic markDirty() #
Notifies HttpTransport instance that there are some requests to be sent and attempts to send them immediately. If a HttpRequest is already running, the new requests will be sent in next "iteration" (after response is received + time interval _delayBetweenRequests passes).
markDirty() {}
dynamic setHandlers(prepareRequest, handleResponse, handleError, [handleDisconnect = null, handleReconnect = null]) #
setHandlers(prepareRequest, handleResponse, handleError, [handleDisconnect = null, handleReconnect = null]) {
super.setHandlers(prepareRequest, handleResponse, handleError, handleDisconnect, handleReconnect);
_timer = new Timer.periodic(this._delayBetweenRequests, (_) => _performRequest());
}