onPacket method

void onPacket(
  1. dynamic packet
)

Called upon transport packet.

Implementation

void onPacket(packet) {
  if ('open' == readyState) {
    // export packet event

    emit('packet', packet);

    // Reset ping timeout on any packet, incoming data is a good sign of
    // other side's liveness
    setPingTimeout();
    switch (packet['type']) {
      case 'ping':
        sendPacket('pong');
        emit('heartbeat');
        break;

      case 'error':
        onClose('parse error');
        break;

      case 'message':
        var data = packet['data'];
        emit('data', data);
        emit('message', data);
        break;
    }
  } else {}
}