为什么这个updateSockets()函数接受一个看起来像这样的参数? [英] Why does this updateSockets() function accept a parameter that look like this?

查看:89
本文介绍了为什么这个updateSockets()函数接受一个看起来像这样的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些在MySQL数据库上执行推送通知的node.js代码。
http:// www。 gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/

I am looking at some node.js code that does push notification on a MySQL database. http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/

有轮询功能。

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')

        }
      });
};

令我困惑的特定代码段就是这个;

The particular code segment above that puzzles me is this;

      updateSockets({
        users: users
      });

为什么参数用户:用户

updateSockets()的代码在这里;

The code for updateSockets() is here;

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);
  });
};


推荐答案

{
    users : users
}

此代码只是一个简单的对象。第一个用户是对象属性的名称,第二个用户只是一个变量。

This code is just a plain objet. The first users is the name of the object property and the second users is just a variable.

如果你愿意,可以这样写:

You can write like this if you want :

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

这篇关于为什么这个updateSockets()函数接受一个看起来像这样的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆