onMessage method

  1. @override
dynamic onMessage(
  1. Payload payload
)
override

Implementation

@override
onMessage(Payload payload) {
  // enabled?
  if (!enabled) return;

  // increment the number of messages received
  _received.set(received + 1);

  // set last serial received
  _serial.set(payload.id);

  // set last message bindable
  _message.set(payload.message);

  // deserialize the data
  Data data = Data.from(payload.message, root: root);

  // if the message didn't deserialize (length 0)
  // is the payload url encoded?
  if (data.isEmpty && payload.message != null) {
    // is a valid url query string?
    String msg = payload.message!.trim();

    // parse the string
    Uri? uri = URI.parse(msg);
    if (uri != null && !uri.hasQuery) uri = URI.parse("?$msg");
    if (uri != null && uri.hasQuery) {
      // add payload url parameters
      Map<String, dynamic> map = <String, dynamic>{};
      uri.queryParameters.forEach((k, v) => map[k] = v);
      if (!map.containsKey('payload')) map['payload'] = payload.message;
      if (!map.containsKey('serial')) map['serial'] = payload.id;
      data.add(map);
    }
  }

  // if the message didn't deserialize (length 0)
  // create a simple map with topic and message bindables <id>.data.topic and <id>.data.message
  // otherwise the data is the deserialized message payload
  if (data.isEmpty) {
    data.insert(0,
        {'id': payload.id, 'serial': payload.id, 'payload': payload.message});
  }

  // fire the onresponse
  onSuccess(data, code: 200);
}