onClose method

void onClose(
  1. dynamic reason, [
  2. dynamic desc
])

Called upon transport close.

@api private

Implementation

void onClose(reason, [desc]) {
  if ('opening' == readyState ||
      'open' == readyState ||
      'closing' == readyState) {
    _logger.fine('socket close with reason: "$reason"');

    // clear timers
    if (pingIntervalTimer != null) {
      pingIntervalTimer!.cancel();
    }
    if (pingTimeoutTimer != null) {
      pingTimeoutTimer!.cancel();
    }

    // stop event from firing again for transport
    transport!.off('close');

    // ensure transport won't stay open
    transport!.close();

    // ignore further transport communication
    transport!.clearListeners();

    // set ready state
    readyState = 'closed';

    // clear session id
    id = null;

    // emit close event
    emit('close', {'reason': reason, 'desc': desc});

    // clean buffers after, so users can still
    // grab the buffers on `close` event
    writeBuffer = [];
    prevBufferLen = 0;
  }
}