onMessage method

void onMessage(
  1. dynamic msg
)

Implementation

void onMessage(msg) {
  // watchStatus sanity check
  switch (this._watchStatus) {
    case WatchStatus.PAUSED:
      {
        // ignore all but error message
        if (msg['msgType'] != 'ERROR') {
          return;
        }
        break;
      }
    case WatchStatus.LOGGINGIN:
    case WatchStatus.INITING:
    case WatchStatus.REBUILDING:
      {
        Console.warn(
            '[realtime listener] internal non-fatal error: unexpected message received while ${this._watchStatus}');
        return;
      }
    case WatchStatus.CLOSED:
      {
        Console.warn(
            '[realtime listener] internal non-fatal error: unexpected message received when the watch has closed');
        return;
      }
    case WatchStatus.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;
      }
  }
}