简体   繁体   English

Socket.io NodeJS 应用程序的实时正常运行时间?

[英]Socket.io live uptime of NodeJS Application?

I have a Node.JS express server running a website, I would like to add a live uptime of the website to the front end for users.我有一个运行网站的 Node.JS Express 服务器,我想为用户在前端添加网站的实时正常运行时间。 Currently I am passing the data to the website when the user requests the page.目前,当用户请求页面时,我将数据传递给网站。 I would like have the uptime show live (update on the page live) I have tried to do this using socket.io with this code.我想要实时显示正常运行时间(在页面上实时更新)我已经尝试使用 socket.io 和此代码来做到这一点。

io.sockets.on('connection', function(sockets){
  var uptime = process.uptime();
  const date = new Date(uptime*1000);
  const days = date.getUTCDate() - 1,
        hours = date.getUTCHours(),
        minutes = date.getUTCMinutes(),
        seconds = date.getUTCSeconds(),
        milliseconds = date.getUTCMilliseconds();
  let segments = [];
  if (days > 0) segments.push(days + ' day' + ((days == 1) ? '' : 's'));
  if (hours > 0) segments.push(hours + ' hour' + ((hours == 1) ? '' : 's'));
  if (minutes > 0) segments.push(minutes + ' minute' + ((minutes == 1) ? '' : 's'));
  if (seconds > 0) segments.push(seconds + ' second' + ((seconds == 1) ? '' : 's'));
  const dateString = segments.join(', ');

  io.sockets.emit('count', {count:dateString})
})

It seems to work for 2 seconds (updating twice) then no more data is sent to the front end.它似乎工作了 2 秒(更新两次),然后不再向前端发送数据。 Any ideas on how I can send process.uptime();关于如何发送process.uptime();的任何想法to the front end?到前端? Cheers干杯

EDIT: I needed to add a setInterval(function() and also update the dateString from a const here is the server updated code,编辑:我需要添加一个setInterval(function()并从 const 更新dateString这里是服务器更新代码,

io.sockets.on('connection', function(sockets){
  setInterval(function(){ 
    var uptime = process.uptime();
    const date = new Date(uptime*1000);
    const days = date.getUTCDate() - 1,
      hours = date.getUTCHours(),
      minutes = date.getUTCMinutes(),
      seconds = date.getUTCSeconds();
    let segments = [];

    if (days > 0) segments.push(days + ' day' + ((days == 1) ? '' : 's'));
    if (hours > 0) segments.push(hours + ' hour' + ((hours == 1) ? '' : 's'));
    if (minutes > 0) segments.push(minutes + ' minute' + ((minutes == 1) ? '' : 's'));
    if (seconds > 0) segments.push(seconds + ' second' + ((seconds == 1) ? '' : 's'));
    dateString = segments.join(', ');
    
    sockets.emit('count',{count:dateString}); }, 1000);
})

And client code和客户端代码

    <script>
      var socket = io();
      socket.on('count', function(data){
        $('#count').html(data.count);
      });
    </script>

Result:结果:

在此处输入图像描述

It seems that you only emit the uptime to the connected clients when a client connects.似乎您只在客户端连接时将正常运行时间发送给连接的客户端。 Every time a client connects, the function under the 'connection' event is being run.每次客户端连接时,都会运行“连接”事件下的 function。

If you want it to run automatically (let's say, every second) you could set an interval that emits the uptime state every second.如果您希望它自动运行(比方说,每秒),您可以设置一个间隔,每秒发出正常运行时间 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM