Я рассматриваю некоторый код node.js, который выводит уведомление в базе данных MySQL. http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/Почему функция updateSockets() принимает параметр, который выглядит так?
Функция опроса.
var pollingLoop = function() {
// Doing the database query
var query = connection.query('SELECT * FROM users'),
users = []; // this array will contain the result of our db query
// setting the query listeners
query
.on('error', function(err) {
// Handle error, and 'end' event will be emitted after this as well
console.log(err);
updateSockets(err);
})
.on('result', function(user) {
// it fills our array looping on each user row inside the db
users.push(user);
})
.on('end', function() {
// loop on itself only if there are sockets still connected
if (connectionsArray.length) {
pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
updateSockets({
users: users
});
} else {
console.log('The server timer was stopped because there are no more socket connections on the app')
}
});
};
Конкретный сегмент кода над этим головоломкой меня таков;
updateSockets({
users: users
});
Почему аргумент users: users
?
Код для обновленияСоздается() здесь;
var updateSockets = function(data) {
// adding the time of the last update
data.time = new Date();
console.log('Pushing new data to the clients connected (connections amount = %s) - %s', connectionsArray.length , data.time);
// sending new data to all the sockets connected
connectionsArray.forEach(function(tmpSocket) {
tmpSocket.volatile.emit('notification', data);
});
};