non può usare l’istanza passata di Socket.IO per classe come ascoltatore di evento

sto creando nodejs app per le partite: quando passo l’istanza presa per la classe di corrispondenza la connessione muore se uno dei client disconnesso e socket ascoltatore nella classe di corrispondenza non funziona mai più

server.ts

class SocketServer {
    public server: Server;
    public matches: Match[] = [];
    private io: socketIo.Server;

    constructor() {
        this.createServer();
        this.sockets();
        this.listen();
    }

    private createServer(): void {
        this.server = createServer();
    }

    private  sockets(): void {
        this.io = socketIo(this.server);
    }

    private listen(): void {
        this.server.listen(this.port, this.host, () => {
            console.log("http://%s:%s", this.host, this.port);
        });

        const nsp = this.io.of("my-namespace")
            .on("connection", (socket: any) => {
            const query = {
                client_id: Number(socket.handshake.query.client_id),
                match: Number(socket.handshake.query.match) };
            socket.join(query.match.toString());
            socket.on("subscribe", (room: string) => {
                socket.join(room);
            });
            socket.on("unsubscribe", (room: string) => {
                socket.leave(room);
            });
            if (this.matches.length > 0) {
                const match = this.findMatchById(query.match);
                if (match === undefined) {
                    this.matches.push(new Match(nsp, socket, query));
                }
            } else {
                this.matches.push(new Match(nsp, socket, query));
            }
        });
    }

 }

nella classe di corrispondenza quando uso io.emit() funziona bene, ma socket.on() non funziona dopo qualsiasi client disconnesso dalla partita

match.ts

export default class Match {
    constructor(namespace: any, socket: any, query: any) {
        this.namespace = namespace;
        this.room = query.match.toString();
        this.id = query.match;
        this.main(socket, query);
    }
    public async main(socket: any, query: any) {
        if (this.initiated === false) {
            await this.initMatch();
        }


        socket.on("player-ready", (data: any) => {
            // some code
        });
        socket.on("disconnect", () => {
            // some code
        });
    }    
}

EN From: can’t use passed instance of socket.io to class as event listener

More similar articles:

1 Comment

  1. ho scoperto dove il problema era, quando sto passando l’istanza socket al costruttore Match, ho lasciato un client collegato alla stanza, e a causa della condizione

    if (this.matches.length > 0) {
        const match = this.findMatchById(query.match);
        if (match === undefined) {
            this.matches.push(new Match(nsp, socket, query));
        }
    } else {
        this.matches.push(new Match(nsp, socket, query));
    }

    non ho mai permesso a nessuno di essere registrato nella stessa partita così ho creato alcuni metodi pubblici nella classe match e usato nel file server come questo:

    match.ts

    public makeClientReady(id: number): void {
        // some code
    }

    server.ts

    const nsp = this.io.of("my-namespace")
            .on("connection", (socket: any) => {
            const query = {
                client_id: Number(socket.handshake.query.client_id),
                match: Number(socket.handshake.query.match) };
            socket.join(query.match.toString());
            socket.on("subscribe", (room: string) => {
                socket.join(room);
            });
            socket.on("unsubscribe", (room: string) => {
                socket.leave(room);
            });
            if (this.matches.length > 0) {
                const match = this.findMatchById(query.match);
                if (match === undefined) {
                    this.matches.push(new Match(nsp, socket, query));
                }
            } else {
                this.matches.push(new Match(nsp, socket, query));
            }
    
            socket.on("player-ready", (data: any) => {
                match.makeClientReady(Number(data.id));
            });
        });

Leave a Reply

Your email address will not be published. Required fields are marked *