R
R
rustler20002012-09-03 23:07:55
JavaScript
rustler2000, 2012-09-03 23:07:55

How to temporarily exclude a socket from participating in an event loop?

Suppose there is an object\array(*A) that stores sockets\connections\streams\... for later\reuse.

If you write a server application, then everything is fine.

If you write a CLI utility, then while the event loop has objects, the node itself will not complete the process.
But I want the node to ignore objects from *A, so as not to control when all asynchronous processes in the utility have completed and kill *A (or the whole process (generally unacceptable)).

How to temporarily exclude objects stored in *A from the event loop and include them in the event loop when necessary?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rustler2000, 2012-09-11
@rustler2000

Node v0.9.1:

var socket = require('net').createConnection(23, function() {
    setTimeout(10000, function() {
        socket.unref(); // Исключили сокет из event loop
        setTimeout(10000, function() {
            // Нет нужды возвращать и закрывать сокет,
            // так как он уже не в event loop
            //socket.ref(); // Вернули сокет в event loop
            //socket.end(); // Закрыли сокет
            console.log('Last message!!!'); /* Когда сообщение будет выведено то нода завершит работу даже если socket все еще имеет соединение (!!!) так как он не участвует в event loop */
        }
    });
});

ref/unref for all HandleWraps, timers, servers, and sockets (Timothy J Fontaine)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question