Skip to content

Commit

Permalink
fix: 增加 wrapper 以避免意外的数据导致爆炸
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Oct 27, 2024
1 parent 8fe11d9 commit a27c70a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,19 @@ export class Server {
}

public setupSocketIO(): void {
const wrapper = function (socket: any, event: string, fn: Function) {
socket.on(event, (...rest: any[]) => {
console.log(rest);
let callback = rest.find((item) => typeof item === "function");
if (!callback) {
console.warn("No callback found in arguments");
}
callback = callback || ((...args: any[]) => {});
const data = callback ? rest.slice(0, rest.indexOf(callback)) : rest;
fn(callback, ...data);
});
};

this.io.use((socket, next) => {
try {
const token = socket.handshake.auth?.token;
Expand Down Expand Up @@ -1189,7 +1202,7 @@ export class Server {
}
});

socket.on('enable', async (data, callback: Function) => {
wrapper(socket, "enable", async (callback: Function, ...data: any) => {
const ack = callback ? callback : (...rest: any[]) => {};
const enableData = data as {
host: string,
Expand Down Expand Up @@ -1314,7 +1327,7 @@ export class Server {
});
});

socket.on('keep-alive', (data, callback: Function) => {
wrapper(socket, "keep-alive", (callback: Function, ...data: any) => {
const ack = callback ? callback : (...rest: any[]) => {};
const keepAliveData = data as {
time: string,
Expand Down Expand Up @@ -1350,7 +1363,7 @@ export class Server {
}
});

socket.on('disable', (data, callback: Function) => {
wrapper(socket, "disable", (callback: Function, ...data: any) => {
const ack = callback ? callback : (...rest: any[]) => {};
const cluster = this.sessionToClusterMap.get(socket.id);

Expand All @@ -1366,7 +1379,7 @@ export class Server {
});

if (Config.instance.enableRequestCertificate) {
socket.on('request-cert', async (callback: Function) => {
wrapper(socket, "request-cert", async (callback: Function) => {
console.log(callback);
const ack = callback ? callback : (...rest: any[]) => {};
console.log(ack);
Expand Down

0 comments on commit a27c70a

Please sign in to comment.