electron,nodejs 如何获取实时网速?

electron 如何获取实时网速?
或者 nodejs 有没有相关的方法?

阅读 3k
1 个回答

如果在用网页端的话,以前常用的方法就是去轮询一个肯定存在的指定大小的文件,javascript计算下载完成的时间,然后计算出结果,
但是现在浏览器有一个实验性的API,Network Information API(浏览器支持度要自己查一下)
https://developer.mozilla.org...
不过实时网速好像也不是很精确

function logConnectionType() {
  let connectionType = 'not supported';
  let downlinkMax = 'not supported';
  let downlink = 'not supported'

  if ('connection' in navigator) {
    connectionType = navigator.connection.effectiveType;

    if ('downlinkMax' in navigator.connection) {
      downlinkMax = navigator.connection.downlinkMax;
    }

    if ('downlink' in navigator.connection) {
        downlink = navigator.connection.downlink
    }
  }

  console.log(`Current connection type: ${connectionType} (downlink max: ${downlinkMax} Mb/s) (downlink: ${downlink} Mb/s)`);
}

logConnectionType();
navigator.connection.addEventListener('change', logConnectionType);

1677388622477.png

你可以在手机上试试,手机上donlinkMax可以被支持
244d4dc0647390bca67182a7d1887ab.jpg

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题