onMessage method
void
onMessage(
- dynamic msg
Implementation
void onMessage(msg) {
// watchStatus sanity check
switch (this._watchStatus) {
case WATCH_STATUS.PAUSED:
{
// ignore all but error message
if (msg['msgType'] != 'ERROR') {
return;
}
break;
}
case WATCH_STATUS.LOGGINGIN:
case WATCH_STATUS.INITING:
case WATCH_STATUS.REBUILDING:
{
Console.warn(
'[realtime listener] internal non-fatal error: unexpected message received while ${this._watchStatus}');
return;
}
case WATCH_STATUS.CLOSED:
{
Console.warn(
'[realtime listener] internal non-fatal error: unexpected message received when the watch has closed');
return;
}
case WATCH_STATUS.ERRORED:
{
Console.warn(
'[realtime listener] internal non-fatal error: unexpected message received when the watch has ended with error');
return;
}
default:
break;
}
if (this._sessionInfo == null) {
Console.warn(
'[realtime listener] internal non-fatal error: sessionInfo not found while message is received.');
return;
}
this._scheduleSendACK();
switch (msg['msgType']) {
case 'NEXT_EVENT':
{
// Console.warn('nextevent ${msg['msgData']['currEvent']} ignored, msg: $msg}');
this._handleServerEvents(msg);
break;
}
case 'CHECK_EVENT':
{
if (this._sessionInfo!.currentEventId < msg['msgData']['currEvent']) {
// client eventID < server eventID:
// there might be one or more pending events not yet received but sent by the server
this._sessionInfo!.expectEventId = msg['msgData']['currEvent'];
this._clearWaitExpectedEvent();
this._waitExpectedTimer = Timer(
Duration(
milliseconds: this._getWaitExpectedTimeoutLength().toInt()),
() {
// must rebuild watch
this._rebuildWatch();
});
Console.log(
'[realtime] waitExpectedTimeoutLength ${this._getWaitExpectedTimeoutLength()}');
}
break;
}
case 'ERROR':
{
// receive server error
this.closeWithError(CloudBaseException(
code: ErrCode.SDK_DATABASE_REALTIME_LISTENER_SERVER_ERROR_MSG,
message:
'${msg['msgData']['code']} - ${msg['msgData']['message']}'));
break;
}
default:
{
Console.warn(
'[realtime listener] virtual client receive unexpected msg: $msg');
break;
}
}
}