page.html 3.0 KB

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