getComet function
Implementation
Future<Comet?> getComet({int? offset}) async {
if (_cometUserChannel != null) {
//Initialize of continue the offset.
if (offset == null) {
offset = _cometOffset;
}
Http.Response? response = await _clientGetRes(_cometUserChannel!.cometServer, queryParameters: { 'offset': offset.toString() });
if (response != null) {
String body = response.body;
//Try substring the json.
String headStr = 'CometChannel.scriptCallback(';
String tailStr = ');';
String jsonStr =
body.substring(headStr.length, body.length - tailStr.length);
dynamic jsonObject = json.decode(jsonStr);
if (jsonObject != null) {
Comet comet = Comet.fromJson(jsonObject);
if (comet.newOffset != null) {
//Check how we should update the comet offset.
if (comet.newOffset == -1) {
//Waiting for data to be posted. Don't update the offset.
} else if (comet.newOffset == -3) {
//Your offset is wrong and you need to resync your data.
_cometOffset = 0;
} else {
//Update the offset.
_cometOffset = comet.newOffset;
}
}
return comet;
} else {
print('Oops! getComet decode json failed?');
return null;
}
} else {
print('Oops! getComet null response or body?');
return null;
}
} else {
print(
'Oops! cometUserChannel is null! Try realtimeGetUserChannel() first to cache a UserChannel!');
return null;
}
}