2015-07-29 2 views
1

Я рассматриваю некоторый код 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); 
    }); 
}; 

ответ

1
{ 
    users : users 
} 

Этот код просто обычный Objet. Первое users - это имя свойства объекта, а второе users - это просто переменная.

Вы можете написать как это, если вы хотите:

var myUsers = users; 
updateSockets({ 
    users: myUsers 
}); 
1

Это дополнительная информация, хранящаяся в данных

Когда этот код выполняет emit(data), он посылает пакет с параметрами user и time (добавлено в updateSockets)

Это сообщение, которое вы хотите отправить

Смежные вопросы