فهرست منبع

Initial commit

Johannes Zellner 9 سال پیش
کامیت
898bcbed9f
4فایلهای تغییر یافته به همراه159 افزوده شده و 0 حذف شده
  1. 1 0
      .gitignore
  2. 76 0
      index.html
  3. 15 0
      package.json
  4. 67 0
      server.js

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+node_modules/

+ 76 - 0
index.html

@@ -0,0 +1,76 @@
+<html>
+
+<head>
+
+<script src="superagent.js"></script>
+
+<style>
+
+body {
+    font-family: Helvetica;
+}
+
+.status {
+    font-size: 22px;
+}
+
+.fail {
+    color: red;
+}
+
+.success {
+    color: green;
+}
+
+</style>
+
+</head>
+
+<body>
+
+<center>
+
+    <h1> Network Status </h1>
+
+    <p>Success rate to Google: <span id="successRateGoogle" class="status">0</span></p>
+    <p>Success rate to local router: <span id="successRateRouter" class="status">0</span></p>
+
+</center>
+
+<script>
+
+function update(elem, data) {
+    var rate = (100 * (data.successCount / data.tests)).toFixed(2);
+
+    elem.innerText = rate + '%';
+
+    if (rate < 90) {
+        elem.classList.add('fail');
+        elem.classList.remove('success');
+    } else {
+        elem.classList.remove('fail');
+        elem.classList.add('success');
+    }
+}
+
+function fetch() {
+    superagent.get('/status', function (error, result) {
+        if (error) {
+            setTimeout(fetch, 1000);
+            return console.error(error);
+        }
+
+        update(document.getElementById('successRateGoogle'), result.body.google);
+        update(document.getElementById('successRateRouter'), result.body.router);
+
+        setTimeout(fetch, 500);
+    });
+}
+
+fetch();
+
+</script>
+
+</body>
+
+</html>

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "network-tester",
+  "version": "1.0.0",
+  "description": "",
+  "main": "server.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "colors": "^1.1.2",
+    "superagent": "^1.8.3"
+  }
+}

+ 67 - 0
server.js

@@ -0,0 +1,67 @@
+#!/usr/bin/env node
+
+'use strict';
+
+require('colors');
+
+var superagent = require('superagent'),
+    http = require('http'),
+    fs = require('fs');
+
+var google = {
+    server: 'https://google.com',
+    tests: 0,
+    failCount: 0,
+    successCount: 0
+};
+
+var router = {
+    server: 'http://192.168.10.1',
+    tests: 0,
+    failCount: 0,
+    successCount: 0
+};
+
+function test(item) {
+    ++item.tests;
+
+    superagent.get(item.server, function (error, result) {
+        if (error) {
+            ++item.failCount;
+            console.log('Error: ', item.server, error.message);
+            // process.stdout.write('×'.red);
+        } else {
+            ++item.successCount;
+            // process.stdout.write('✓'.green);
+        }
+    });
+}
+
+setInterval(test.bind(null, google), 500);
+setInterval(test.bind(null, router), 500);
+
+function status(req, res) {
+    var body = {
+        google: google,
+        router: router
+    };
+
+    res.writeHead(200, { 'Content-Type': 'application/json' });
+    res.end(JSON.stringify(body));
+}
+
+function sendSuperagent(req, res) {
+    res.writeHead(200, { 'Content-Type': 'application/javascript' });
+    res.end(fs.readFileSync('node_modules/superagent/superagent.js'));
+}
+
+http.createServer(function (req, res) {
+    if (req.url === '/status') return status(req, res);
+    if (req.url === '/superagent.js') return sendSuperagent(req, res);
+
+    res.writeHead(200, { 'Content-Type': 'text/html' });
+    res.end(fs.readFileSync('index.html'));
+}).listen(8000);
+
+console.log('Status page available on http://localhost:8000');
+