getCometByUrl function
Implementation
Future<Comet?> getCometByUrl(String cometUrlServer, {int offset = 0}) async {
// We are replacing the default offset to our cached offset here.
String serverUriDefaultOffsetStr = 'offset=0';
String cometUriWithCachedOffset = cometUrlServer.substring(
0,
cometUrlServer.length -
serverUriDefaultOffsetStr.length) +
'offset=' +
offset.toString();
//We should use an independent Http client.
Http.Response response = await Http.get(Uri.parse(cometUriWithCachedOffset));
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) {
return Comet.fromJson(jsonObject);
} else {
print('Oops! getComet decode json failed?');
return null;
}
}