So, I'm pretty new to JavaScript, and while I know the little tricks to do things like this in Java the same tricks don't really apply here, or at least I can't really figure out how to use them, so I'm currently using two objects in the form of collections:
var ConnectedClients_socket = {};
var ConnectedClients_networkId = {};
The hold a key/value pair to link a networkID
to a client and then a duplicated object that uses the socket as a key. Any optimizations would be great, I'll be grabbing the client by the socket and ID a lot throughout the application.
On to the problem:
Upon disconnection I need to remove the client instance from the ConnectedClients_networkId
object. From my understanding this is done using the delete
keyword.
Here's the code I'm currently trying to use for this:
console.log('socket disconnected');
delete Client.ConnectedClients_networkId[Client.ConnectedClients_socket[socket].getNetworkId()];
delete Client.ConnectedClients_socket.socket;
However this is giving me the following error:
TypeError: Cannot call method 'getNetworkId' of undefined
which shows that
Client.ConnectedClients_socket[socket]
is returning undefined, while Client.ConnectedClients_socket.socket
is being removed successfully a line later. I've looked up a few different pages on getting a value from a key in JavaScript, and I don't see what I'm doing wrong.
Here's the constructor for a client:
var Client = function(networkId, socket, ...) {
this.networkId = networkId;
this.socket = socket;
...
ConnectedClients_socket.socket = this;
ConnectedClients_networkId.networkId = this;
}