Async show IP
异步显示ip

ustc有个异步加载的“您的IP地址”,是用CGI实现的。php版实现如下:

naive php:

var ipv4addr= document.getElementById("ipv4addr"); ipv4addr.innerHTML='<?=$_SERVER['REMOTE_ADDR'];?>';

or workerman:

composer require workerman/workerman
<?php
require_once __DIR__ . '/vendor/autoload.php';
$http_worker = new Workerman\Worker("http://0.0.0.0:2345");
$http_worker->count = 4;
$http_worker->onMessage = function(Workerman\Connection\TcpConnection $connection, Workerman\Protocols\Http\Request $request)
{
    $connection->close(
      sprintf('var ipv4addr= document.getElementById("ipv4addr"); 
      ipv4addr.innerHTML=%s', $connection->getRemoteIp())
    );
};
Workerman\Worker::runAll();

or cloudflare workers?

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  const url = new URL(request.url);
  if (url.pathname.match('/myipv4addr')) {
    const $body = `var ipv4addr= document.getElementById("ipv4addr"); ipv4addr.innerHTML='${request.headers.get("cf-connecting-ip")}';\n`
    return new Response($body, {
      status: 200,
      headers: { "Content-Type": "text/plain;charset=UTF-8" },
    })
  } else {
    return new Response('Not found', { status: 404 });
  }
}

ab result:

[root@localhost ~]# ab -c1000 -n100000 http://127.0.0.1:2345/
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:        workerman
Server Hostname:        127.0.0.1
Server Port:            2345

Document Path:          /
Document Length:        79 bytes

Concurrency Level:      1000
Time taken for tests:   8.112 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      20000000 bytes
HTML transferred:       7900000 bytes
Requests per second:    12327.60 [#/sec] (mean)
Time per request:       81.119 [ms] (mean)
Time per request:       0.081 [ms] (mean, across all concurrent requests)
Transfer rate:          2407.73 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   39  13.3     39      81
Processing:    18   41  13.2     41      84
Waiting:        1   19  15.3     13      72
Total:         39   81   5.3     79     100

Percentage of the requests served within a certain time (ms)
  50%     79
  66%     82
  75%     83
  80%     85
  90%     87
  95%     90
  98%     93
  99%     95
 100%    100 (longest request)

html

<script language="JavaScript" type="text/javascript" src=http://202.38.64.1/cgi-bin/myipv4addr></script>
您的IP地址是:<div id="ipv4addr"></div>

css

div{ 
    display:inline;
}

灵活地使用:

<div id="ipv4addr" style="display:inline"></div>

参考资料:

  1. http://ns.ustc.edu.cn/
  2. https://www.workerman.net/doc/workerman/getting-started/simple-example.html

最后修改于 Sun, 08 Jan 2017