dh_ackergaul
vor 3 Tagen 5bbf43c1b146439ab882815c12ed6292f1d7b4df
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
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>pending</title>
</head>
<body style="background-color: #585858;">
  <script>
    const PORT = new URL(document.location.href).searchParams.get("port");
 
    // ===== DhPostMessageDll In-Process Ping-Pong (kein localhost-Webserver noetig) =====
    // Antwort von C++ kommt hier an (erwartet "pong:ping").
    window.dhOnMessage = function (msg) {
      if (msg == "pong:ping");
      document.title = "#success#CefSharp.PostMessage.Ping#";
    };
    function dhSendPing() {
      try {
        if (window.CefSharp && window.CefSharp.PostMessage) {
          CefSharp.PostMessage("ping");
        } else {
          document.title = "#failure#CefSharp.PostMessage nicht verfuegbar#";
        }
      } catch (e) {
        document.title = "#failure#CefSharp.PostMessage " + e.message + "#";
      }
    }
    window.addEventListener("load", dhSendPing);
 
    function pingTestLocal(callback) {
      try {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "http://127.0.0.1:" + PORT + "/api/ping", true);
        xhr.timeout = 5000;
 
        var called = false;
        function done(result) {
          if (!called) {
            called = true;
            callback(result);
          }
        }
 
        xhr.ontimeout = function () {
          done({ status: 0, statusText: "timeout" });
        };
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.responseText === "ping") {
              callback({ status: xhr.status, statusText: "ok" });
            } else {
              callback({ status: xhr.status, statusText: xhr.statusText });
            }
          }
        };
        xhr.send(null);
      } catch (err) {
        callback({ status: 0, statusText: err.message });
      }
    }
 
    function pingWithRetry(attemptsLeft, delays, callback) {
      pingTestLocal(function (result) {
        if (result.status === 200 || attemptsLeft <= 1) {
          callback(result, delays.length - attemptsLeft + 1);
        } else {
          var delay = delays[delays.length - attemptsLeft + 1] || 5000;
          setTimeout(function () {
            pingWithRetry(attemptsLeft - 1, delays, callback);
          }, delay);
        }
      });
    }
 
    function onPingResult(result, attempts) {
      if(result.status === 200) {
        document.title = "#success#" + attempts + "#";
      } else {
        document.title = "#failure#" + result.status + "#" + result.statusText + "#";
      }
    }
 
    function closeWindow() {
      document.title = "#close#";
    }
 
    setTimeout(function () {
        pingWithRetry(3, [0, 2000, 5000], onPingResult);
        setTimeout(function () {
          closeWindow();
        }, 15000);
    }, 30000);
  </script>
</body>
</html>