socketio / socket.io-website

Socket.IO website and blog
https://socket.io
319 stars 671 forks source link

Remove `upgrade` event from example #369

Closed samlior closed 1 year ago

samlior commented 1 year ago

According to engine.io doc, it looks like the socket no longer emits the upgrade event, so it should be removed from this example.

By the way, I would like to ask how I should add some verification information in the upgrade request header, and reject the upgrade if the verification information is illegal.

darrachequesne commented 1 year ago

Hi! The upgrade event was indeed missing from the documentation, this should be fixed now (https://github.com/socketio/engine.io/commit/535b068670848f6a3193528582484826a858d680).

how I should add some verification information in the upgrade request header, and reject the upgrade if the verification information is illegal.

You can manually handle the upgrade event of the Node.js HTTP server:

import { createServer } from "http";
import { Server } from "socket.io";

const httpServer = createServer();
const io = new Server(httpServer);

httpServer.removeAllListeners("upgrade");

httpServer.on("upgrade", (req, socket, head) => {
  // do some verification...

  // then
  io.engine.handleUpgrade(req, socket, head);
});

httpServer.listen(3000);

Reference: https://nodejs.org/api/http.html#event-upgrade_1

samlior commented 1 year ago

Thank you so much!