mx 3 anni fa
parent
commit
0befb6a6fd
1 ha cambiato i file con 28 aggiunte e 0 eliminazioni
  1. 28 0
      app/view/index/view.html

+ 28 - 0
app/view/index/view.html

@@ -11,4 +11,32 @@
 <body>
 <?=htmlspecialchars($hello)?>
 </body>
+<script>
+    function connect() {
+        // 与服务端建立WebSocket连接
+        //(为了方便测试这里ip使用的是127.0.0.1,正式环境请使用外网ip)
+        ws = new WebSocket('ws://127.0.0.1:8888');
+        // 连接建立后发送daping,表明自己是电脑浏览器
+        ws.onopen = function() {
+            ws.send('daping');
+        };
+        //  收到服务端推送的数据后,将数据显示在浏览器里(心跳数据pong除外)
+        ws.onmessage = function (e) {
+            if (e.data !== 'pong') {
+                console.log(e.data)
+            }
+        };
+        // 没隔50秒发送一个心跳数据 ping 给服务器,保持连接
+        ws.timer = setInterval(function () {
+            ws.send('ping');
+        }, 5000);
+        //  当连接关闭时清除定时器,并设置1秒后重连
+        ws.onclose = function () {
+            clearTimeout(ws.timer);
+            setTimeout(connect, 1000);
+        };
+    }
+    // 执行连接
+    connect();
+</script>
 </html>