onPacket method

void onPacket(
  1. Map packet
)

Handles a packet.

@api private

Implementation

void onPacket(Map packet) {
  if ('opening' == readyState ||
      'open' == readyState ||
      'closing' == readyState) {
    var type = packet['type'];
    var data = packet['data'];
    _logger.fine('socket receive: type "$type", data "$data"');

    emit('packet', packet);

    // Socket is live - any packet counts
    emit('heartbeat');

    switch (type) {
      case 'open':
        onHandshake(json.decode(data ?? 'null'));
        break;

      case 'pong':
        setPing();
        emit('pong');
        break;

      case 'error':
        onError({'error': 'server error', 'code': data});
        break;

      case 'message':
        emit('data', data);
        emit('message', data);
        break;
    }
  } else {
    _logger.fine('packet received with socket readyState "$readyState"');
  }
}