fix: handle late HTTP server binding
The following example:
```js
const io = new Server(); // without port or HTTP server
instrument(io, {
auth: false
});
io.listen(3000);
```
will now work properly.
Related: https://github.com/socketio/socket.io-admin-ui/issues/49
This commit is contained in:
parent
6d58a755b4
commit
b21b649ea2
19
lib/index.ts
19
lib/index.ts
|
|
@ -142,7 +142,7 @@ const initStatsEmitter = (
|
|||
"server_stats",
|
||||
Object.assign({}, baseStats, {
|
||||
uptime: process.uptime(),
|
||||
clientsCount: io.engine.clientsCount,
|
||||
clientsCount: io.engine?.clientsCount,
|
||||
pollingClientsCount: io._pollingClientsCount,
|
||||
aggregatedEvents: io._eventBuffer.getValuesAndClear(),
|
||||
namespaces,
|
||||
|
|
@ -489,7 +489,7 @@ const registerEngineListeners = (io: Server) => {
|
|||
io._eventBuffer = new EventBuffer();
|
||||
io._pollingClientsCount = 0;
|
||||
|
||||
io.engine.on("connection", (rawSocket: any) => {
|
||||
const onConnection = (rawSocket: any) => {
|
||||
io._eventBuffer.push("rawConnection");
|
||||
|
||||
if (rawSocket.transport.name === "polling") {
|
||||
|
|
@ -524,7 +524,20 @@ const registerEngineListeners = (io: Server) => {
|
|||
rawSocket.on("close", (reason: string) => {
|
||||
io._eventBuffer.push("rawDisconnection", reason);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
if (io.engine) {
|
||||
io.engine.on("connection", onConnection);
|
||||
} else {
|
||||
// io.engine might be undefined if instrument() is called before binding the Socket.IO server to the HTTP server
|
||||
process.nextTick(() => {
|
||||
if (io.engine) {
|
||||
io.engine.on("connection", onConnection);
|
||||
} else {
|
||||
debug("WARN: no engine");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export function instrument(io: Server, opts: Partial<InstrumentOptions>) {
|
||||
|
|
|
|||
|
|
@ -67,6 +67,17 @@ describe("Socket.IO Admin (server instrumentation)", () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("should work with io.listen()", () => {
|
||||
const io = new Server();
|
||||
|
||||
instrument(io, {
|
||||
auth: false,
|
||||
});
|
||||
|
||||
io.listen(0);
|
||||
io.close();
|
||||
});
|
||||
|
||||
describe("authentication", () => {
|
||||
it("prevents anonymous connection", (done) => {
|
||||
instrument(io, {
|
||||
|
|
|
|||
Loading…
Reference in New Issue