Deleting namespace in Socket IO

657 views Asked by At

This question has been asked before here, but I've implemented the answer and the problem has persisted.

I'm working in typescript and I have a Room class that encapsulates a namespace (among other things), and a RoomManager class that maintains a list of rooms. I recently added functionality where a room is removed from the list of rooms upon the user count reaching 0. Inside the room, it sets the namespace field to null. The room is removed from the manager. However, when I create the namespace again later, I run into problems that I've found indicative of multiple namespaces with the same name.

The linked answer stated that removing the namespace from the array would delete it, but something about my situation is preventing the namespace from disappearing. I don't know a lot about garbage collecting, but that's my uneducated guess at where the problem lies. I'd be happy to post some code and more specifics if needed.

EDIT: adding some code: Room constructor:

constructor(namespace: SocketIO.Namespace, manager: roomManager) {
    this.users = new Array();
    this.manager = manager;
    var room = this;
    namespace.on('connection', function(socket) {
    //socket events that use the room reference to the object to complete tasks
    }
    this.namespace = namespace;
}

Function that removes the namespace and one that removes the room from array:

removeUser(user: User){
    var index: number = this.users.indexOf(user);
    if (index > -1){
        this.users.splice(index, 1);
    }
    if (this.users.length < 1){
        this.namespace = null;
        this.manager.removeRoom(this);
    }
}

removeRoom(room: Room){
    var index: number = this.rooms.indexOf(room);
    if (index > -1){
        this.rooms.splice(index, 1);
    }
}
1

There are 1 answers

0
Nicolas Peckman On BEST ANSWER

For anyone else who has this specific problem, I found the answer. I misunderstood the linked question: I didn't realize the array that was being discussed was located in the Server object in SocketIO, not an outside array. Furthermore, at the time of this post, Typescript's SocketIO .d.ts file doesn't define the nsps array that exists in the Server object. I solved the problem by adding the property to my .d.ts file and following the deletion instructions outlined in the linked question.