handshake method

void handshake(
  1. String transportName,
  2. SocketConnect connect
)

Handshakes a new client.

Implementation

void handshake(String transportName, SocketConnect connect) {
  var id = generateId(connect);

  Transport transport;
  var req = connect.request;
  try {
    transport = Transports.newInstance(transportName, connect);
    if ('polling' == transportName) {
      transport.maxHttpBufferSize = maxHttpBufferSize;
      transport.httpCompression = httpCompression;
    } else if ('websocket' == transportName) {
      transport.perMessageDeflate = perMessageDeflate;
    }

    if (req.uri.hasQuery && req.uri.queryParameters.containsKey('b64')) {
      transport.supportsBinary = false;
    } else {
      transport.supportsBinary = true;
    }
  } catch (e) {
    sendErrorMessage(req, ServerErrors.badRequest);
    return;
  }
  var socket = Socket(id, this, transport, connect);

  if (cookie?.isNotEmpty == true) {
    transport.on('headers', (headers) {
      headers['Set-Cookie'] = '$cookie=${Uri.encodeComponent(id)}' +
          (cookiePath?.isNotEmpty == true ? '; Path=$cookiePath' : '') +
          (cookiePath?.isNotEmpty == true && cookieHttpOnly == true
              ? '; HttpOnly'
              : '');
    });
  }

  transport.onRequest(connect);

  clients[id] = socket;
  clientsCount++;

  socket.once('close', (_) {
    clients.remove(id);
    clientsCount--;
  });

  emit('connection', socket);
}