close method

Socket close()

Closes the connection.

@api private

Implementation

Socket close() {
  var close = () {
    onClose('forced close');
    _logger.fine('socket closing - telling transport to close');
    transport!.close();
  };

  var temp;
  var cleanupAndClose = (_) {
    off('upgrade', temp);
    off('upgradeError', temp);
    close();
  };

  // a workaround for dart to access the local variable;
  temp = cleanupAndClose;

  var waitForUpgrade = () {
    // wait for upgrade to finish since we can't send packets while pausing a transport
    once('upgrade', cleanupAndClose);
    once('upgradeError', cleanupAndClose);
  };

  if ('opening' == readyState || 'open' == readyState) {
    readyState = 'closing';

    if (writeBuffer.isNotEmpty) {
      once('drain', (_) {
        if (upgrading == true) {
          waitForUpgrade();
        } else {
          close();
        }
      });
    } else if (upgrading == true) {
      waitForUpgrade();
    } else {
      close();
    }
  }

  return this;
}