C
C
Caelwyn2016-01-05 11:20:54
JavaScript
Caelwyn, 2016-01-05 11:20:54

How to achieve object deletion in JS?

Started learning JS, decided to start with a simple chat. Actually there was a problem.
So, I have a ChatServer object, which implements a socket.io object, and implements work with connecting and disconnecting clients.

"use strict";

var SocketServer = require('socket.io')
    , Client = require('./Client.js');

class ChatServer {
    constructor(app, http) {
        var io = SocketServer(http)
            , clients = {};

        io.on('connection', function(socket) {
            console.log('Client ' + socket.id + ' connected');

            clients[socket.id] = new Client(app, socket);

            socket.on('disconnect', function() {
                console.log('Client ' + socket.id + ' disconnected');

                clients[socket.id] = undefined;
                delete clients[socket.id];
            });
        });
    }
}

module.exports = ChatServer;

When a client connects, a Client object is created in which message processing is implemented for this specific client.
"use strict";

class Client {
    constructor(app, socket) {
        this.app = app;
        this.socket = socket;

        this.app.on('chat.sendMessage', this.sendMessage.bind(this));
    }

    sendMessage(message) {
        console.log('Client ' + this.socket.id + ' on chat.sendMessage');
        this.socket.emit('chat.sendMessage', message);
    }
}

module.exports = Client;

app is an EventEmitter from which chat.sendMessage messages are periodically received (for testing), messages are received in clients and sent to the browser.
However, when the client connects, the client object does not go anywhere, and continues to catch messages and send them to the disconnected socket. As I understand it, a disconnected socket also does not go anywhere and differs only in the connected / disconnected properties.
ZbEFj8ykXba9CxLiAAAA the connected
Client Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client is disconnected ZbEFj8ykXba9CxLiAAAA
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
Client ZbEFj8ykXba9CxLiAAAA on chat.sendMessage
I, apparently, do not fully understand the mechanics of attaching objects, I know that for this you need to remove all references to it, what, here and done.
clients[socket.id] = undefined;
                delete clients[socket.id];

However, apparently, this is not enough. So the question is, what can be done about it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Deleted Deleted, 2016-01-05
@Caelwyn

clients[socket.id] = undefined;
delete clients[socket.id];

You are doing the same thing twice here.
All right, because in the client's constructor you have subscribed to the event:
but forgot to unsubscribe when disconnected.

I
Ivanq, 2016-01-05
@Ivanq

Least,

clients[socket.id] = undefined;
delete clients[socket.id];

can not. This is how you delete . You need to remove the first line:
delete clients[socket.id];

A
Alexey Strukov, 2016-01-05
@pm_wanderer

Is it possible to delete an object in Javascript by removing all references to it?
This kind of works in ARC only.
If in js the garbage collector is responsible for objects, then most likely he will delete them himself.
And the fact that the object continues to catch messages, then apparently it is necessary to prohibit it from doing it in another way, except for deletion

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question