aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/webapi/network_stats/index.html
blob: c9aa8a94c10097de822b1b2c3005525ca6f67783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
title: 網路流量統計
slug: WebAPI/Network_Stats
translation_of: Archive/B2G_OS/API/Network_Stats_API
---
<p>{{ non-standard_header() }}</p>
<p>{{ B2GOnlyHeader2('certified') }}</p>
<h2 id="摘要">摘要</h2>
<p>Network Stats API 將監控資料用量,並將此數據提供給 Certified Apps。</p>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.mozNetworkStats" title="Returns a MozNetworkStatsManager object you can use to access data amount information."><code>navigator.mozNetworkStats</code></a> 屬於 <a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStatsManager" title="The MozNetworkStatsManager interface provides methods and properties to monitor data usage."><code>MozNetworkStatsManager</code></a> 介面的實體 (Instance)。透過 <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.mozNetworkStats" title="Returns a MozNetworkStatsManager object you can use to access data amount information."><code>navigator.mozNetworkStats</code></a> 即可取得相關資料。</p>
<h2 id="存取資料">存取資料</h2>
<p>有關已接收/已傳送資料量的資訊,均將由系統自動儲存。而 <a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStatsManager.getNetworkStats" title="The getNetworkStats method allows to retrieve the data usage statistics for the various connection types."><code>MozNetworkStatsManager.getNetworkStats()</code></a> 函式可存取相關資訊。此函式的第一組參數屬於組態物件,且必備下列屬性:</p>
<ul>
  <li><code>start</code>:資料物件,代表資料統計開始。</li>
  <li><code>end</code>:資料物件,代表資料統計結束。</li>
  <li><code>connectionType</code>:資料來源,亦即連線類型。可能為「<code>wifi</code><code></code><code>mobile</code><code></code><code>null</code><code>。若為</code> <code>null,則會合併此 2 項來源的資料統計量。若要事先知道資料來源是否有效,則 </code><a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStatsManager.connectionTypes" title="The connectionTypes property is a list of all possible monitored connection type by the current device."><code>MozNetworkStatsManager.connectionTypes</code></a> 屬性可回傳由字串 (代表各種支援的連線類型) 所構成的 <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="/en-US/docs/JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></li>
</ul>
<p>一旦呼叫此函式,隨即回傳 <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMRequest" title="A DOMRequest object represents an ongoing operation. It provides callbacks that are called when the operation completes, as well as a reference to the operation's result. A DOM method that initiates an ongoing operation may return a DOMRequest object that"><code>DOMRequest</code></a> 以處理資訊請求的成功或失敗狀態。若為成功狀態,則請求的 <code>result</code> 即為 <a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStats" title="The MozNetworkStats objects gives access to statistics about the data usage for a given network."><code>MozNetworkStats</code></a> 物件。</p>
<pre class="brush: js">var manageWifi   = navigator.mozNetworkStats.connectionTypes.indexOf('wifi')   &gt; -1;
var manageMobile = navigator.mozNetworkStats.connectionTypes.indexOf('mobile') &gt; -1;

var config = {
  start: new Date(),
  end  : new Date(),
  connectionType: manageWifi ? 'wifi' : null
};

var request = navigator.mozNetworkStats.getNetworkStats(config);

request.onsuccess = function () {
  console.log("Data received: " + request.result.data[0].rxBytes + " bytes");
  console.log("Data sent: " + request.result.data[0].txBytes + " bytes")
}

request.onerror = function () {
  console.log("Something goes wrong: ", request.error);
}
</pre>
<h2 id="隨著時間連續取樣">隨著時間連續取樣</h2>
<p>資料總量的相關資訊均儲存於區塊 (Chunk) 中,以利隨時查看資料流量。各個區塊均為 1 組值,代表上個區塊儲存之後所交換的資料總量。</p>
<p>在請求統計資料時,<a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStats" title="The MozNetworkStats objects gives access to statistics about the data usage for a given network."><code>MozNetworkStats</code></a> 物件將針對 <code>start</code><code>end</code> 日期之間所定義的間隔,儘量攜帶夠多的資料區塊。區塊的總數將因下列 2 組參數 (均屬唯讀參數) 而有所變化:</p>
<ul>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStatsManager.sampleRate" title="The sampleRate property indicates the time in second between two data usage chunk are stored."><code>MozNetworkStatsManager.sampleRate</code></a> 代表 2 組區塊之間的時間,以秒計算。</li>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStatsManager.maxStorageSamples" title="The maxStorageSamples property indicates the maximum number of date usage chunk stored for each type of connection."><code>MozNetworkStatsManager.maxStorageSamples</code></a> 代表各種連線類型的最大區塊數量。</li>
</ul>
<p>各個資料區塊均為 1 個 <a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStatsData" title="The MozNetworkStatsData objects represent a chunk of data usage statistics."><code>MozNetworkStatsData</code></a> 物件。透過<a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStats.data" title="The data property is a list of data usage statistics chunk. Each chunk is a MozNetworkStatsData providing the total amount a bytes received and send during a given amount of time (this amount of time is accessible through the MozNetworkStatsManager.sample"><code>MozNetworkStats.data</code></a> 屬性即可取得各個既定時間框架 (Time frame) 的所有資料區塊,進而形成 <a href="https://developer.mozilla.org/en-US/docs/Web/API/MozNetworkStatsData" title="The MozNetworkStatsData objects represent a chunk of data usage statistics."><code>MozNetworkStatsData</code></a> 物件的 <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="/en-US/docs/JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></p>
<pre class="brush: js">var rate = navigator.mozNetworkStats.sampleRate;
var max  = navigator.mozNetworkStats.maxStorageSample;

var config = {
  start: new Date() - (rate * max), // This allows to get all the available data chunks.
  end  : new Date(),
  connectionType: 'mobile'
};

var request = navigator.mozNetworkStats.getNetworkStats(config);

request.onsuccess = function () {
  var total = {
    receive: 0,
    send   : 0
  };

  this.result.forEach(function (chunk) {
    total.receive += chunk.rxBytes;
    total.send    += chunk.txBytes;
  });

  console.log("Since: " + config.start.toString());
  console.log("Data received: " + (total.receive * 1000).toFixed(2) + "Ko");
  console.log("Data sent: " + (total.send * 1000).toFixed(2) + "Ko")
}

request.onerror = function () {
  console.log("Something goes wrong: ", request.error);
}</pre>
<h2 id="規格">規格</h2>
<p>尚無任何規格。</p>
<h2 id="另可參閱">另可參閱</h2>
<ul>
  <li>{{domxref("window.navigator.mozNetworkStats","navigator.mozNetworkStats")}}</li>
  <li>{{domxref("MozNetworkStats")}}</li>
  <li>{{domxref("MozNetworkStatsData")}}</li>
  <li>{{domxref("MozNetworkStatsManager")}}</li>
  <li><a href="/en-US/docs/WebAPI/Network_Stats_2_0_proposal" title="/en-US/docs/WebAPI/Network_Stats_2_0_proposal">NetworkStats API 2.0 proposal</a></li>
</ul>