MCPcopy
hub / github.com/socketio/socket.io / handshake

Function handshake

packages/engine.io/lib/server.ts:435–528  ·  view source on GitHub ↗

* Handshakes a new client. * * @param {String} transportName * @param {Object} req - the request object * @param {Function} closeConnection * * @protected

(
    transportName: TransportName,
    req: EngineRequest,
    closeConnection: ErrorCallback,
  )

Source from the content-addressed store, hash-verified

433 * @protected
434 */
435 protected async handshake(
436 transportName: TransportName,
437 req: EngineRequest,
438 closeConnection: ErrorCallback,
439 ) {
440 const protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default
441 if (protocol === 3 && !this.opts.allowEIO3) {
442 debug("unsupported protocol version");
443 this.emit("connection_error", {
444 req,
445 code: Server.errors.UNSUPPORTED_PROTOCOL_VERSION,
446 message:
447 Server.errorMessages[Server.errors.UNSUPPORTED_PROTOCOL_VERSION],
448 context: {
449 protocol,
450 },
451 });
452 closeConnection(Server.errors.UNSUPPORTED_PROTOCOL_VERSION);
453 return;
454 }
455
456 let id;
457 try {
458 id = await this.generateId(req);
459 } catch (e) {
460 debug("error while generating an id");
461 this.emit("connection_error", {
462 req,
463 code: Server.errors.BAD_REQUEST,
464 message: Server.errorMessages[Server.errors.BAD_REQUEST],
465 context: {
466 name: "ID_GENERATION_ERROR",
467 error: e,
468 },
469 });
470 closeConnection(Server.errors.BAD_REQUEST);
471 return;
472 }
473
474 debug('handshaking client "%s"', id);
475
476 try {
477 var transport = this.createTransport(transportName, req);
478 if ("polling" === transportName) {
479 transport.maxHttpBufferSize = this.opts.maxHttpBufferSize;
480 transport.httpCompression = this.opts.httpCompression;
481 } else if ("websocket" === transportName) {
482 transport.perMessageDeflate = this.opts.perMessageDeflate;
483 }
484 } catch (e) {
485 debug('error handshaking to transport "%s"', transportName);
486 this.emit("connection_error", {
487 req,
488 code: Server.errors.BAD_REQUEST,
489 message: Server.errorMessages[Server.errors.BAD_REQUEST],
490 context: {
491 name: "TRANSPORT_HANDSHAKE_ERROR",
492 error: e,

Callers

nothing calls this directly

Calls 5

debugFunction · 0.85
emitMethod · 0.65
createTransportMethod · 0.45
onMethod · 0.45
onRequestMethod · 0.45

Tested by

no test coverage detected