page.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>大屏首页</title>
  6. <style>
  7. body,
  8. p,
  9. a {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. body {
  14. background: #ab0b0b url(img/pbg.jpg) no-repeat center top;
  15. background-size: 100% auto;
  16. }
  17. html,
  18. body {
  19. width: 100%;
  20. height: 100%;
  21. }
  22. .full {
  23. position: absolute;
  24. top: 0;
  25. left: 0;
  26. right: 0;
  27. bottom: 0;
  28. background-color: rgba(0, 0, 0, 0.6);
  29. }
  30. .p-top {
  31. width: 80%;
  32. height: auto;
  33. display: block;
  34. margin: 50px auto;
  35. }
  36. .text {
  37. font-size: 20px;
  38. color: #fff;
  39. margin: 200px auto 50px;
  40. width: 50%;
  41. }
  42. .btn-box {
  43. margin: 0 auto;
  44. width: 50%;
  45. display: flex;
  46. justify-content: space-around;
  47. }
  48. .btn-box a {
  49. display: block;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="full">
  55. <!-- <img src="img/ptop.png" class="p-top" alt=""> -->
  56. <p class="text">当前在线人数:<span id="nowWait"></span></p>
  57. <div class="btn-box">
  58. <a onclick="setStatus(1)" class="btn1"><img src="img/pbtn.png"></a>
  59. <a onclick="setStatus(0)" class="btn2"><img src="img/pbtn2.png"></a>
  60. </div>
  61. </div>
  62. <script src="js/jquery.js"></script>
  63. <script>
  64. var limitConnect = 3; // 断线重连次数
  65. var timeConnect = 0;
  66. var baseUrl = "47.108.130.28:9000";
  67. var wsWaitUrl = "ws://" + baseUrl + "/ws/quepass/onlinewaiting";
  68. function webSocketInit(service) {
  69. var ws = new WebSocket(service);
  70. var timer = null
  71. ws.onopen = function () {
  72. console.log("open success");
  73. localStorage.removeItem("lockReconnect");
  74. //定时发送
  75. timer = window.setInterval(function () {
  76. ws.send("hello")
  77. }, 1000)
  78. }
  79. var $nowWait = $("#nowWait");
  80. ws.onmessage = function (e) {
  81. var res = JSON.parse(e.data);
  82. var len = res.data?res.data.length:0;
  83. if (len >= 0) {
  84. $nowWait.html(len);
  85. }
  86. }
  87. ws.onclose = function () {
  88. console.log("closed...")
  89. window.clearInterval(timer)
  90. reconnect(service)
  91. }
  92. ws.onerror = function (err) {
  93. window.clearInterval(timer)
  94. reconnect(service);
  95. };
  96. return ws
  97. }
  98. // 重连
  99. function reconnect(service) {
  100. // lockReconnect加锁,防止onclose、onerror两次重连
  101. if (limitConnect > 0) {
  102. if (localStorage.getItem('lockReconnect') != true) {
  103. localStorage.setItem("lockReconnect", 1);
  104. limitConnect--;
  105. timeConnect++;
  106. console.log("第" + timeConnect + "次重连");
  107. // 进行重连
  108. setTimeout(function () {
  109. webSocketInit(service);
  110. localStorage.removeItem("lockReconnect");
  111. }, 2000);
  112. }
  113. } else {
  114. console.log("TCP连接已超时");
  115. }
  116. }
  117. // 初始化ws
  118. var ws = webSocketInit(wsWaitUrl)
  119. // 开始或结束游戏
  120. function setStatus(num) {
  121. $.post("http://47.108.130.28:9000/api/quepass/status", JSON.stringify({ action: num }));
  122. }
  123. </script>
  124. </body>
  125. </html>