page.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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: 900px;
  42. }
  43. .btn-box {
  44. margin: 0 auto;
  45. width: 900px;
  46. display: flex;
  47. justify-content: space-around;
  48. }
  49. .btn-box a {
  50. display: block;
  51. }
  52. .btn-box img{
  53. display: block;
  54. width:274px;
  55. height:auto;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="full">
  61. <!-- <img src="img/ptop.png" class="p-top" alt=""> -->
  62. <p class="text">当前在线人数:<span id="nowWait">0</span></p>
  63. <div class="btn-box">
  64. <a onclick="setStatus(1)" class="btn1"><img src="img/pbtn.png"></a>
  65. <a onclick="setStatus(2)" class="btn2"><img src="img/pbtn2.png"></a>
  66. </div>
  67. </div>
  68. <script src="js/jquery.js"></script>
  69. <script>
  70. var limitConnect = 3; // 断线重连次数
  71. var timeConnect = 0;
  72. var baseUrl = "gj.100t.com";
  73. var wsWaitUrl = "ws://" + baseUrl + "/ws/quepass/onlinewaiting/1000";
  74. var startGameUrl="http://"+baseUrl+"/api/quepass/status";
  75. function webSocketInit(service) {
  76. var ws = new WebSocket(service);
  77. var timer = null
  78. ws.onopen = function () {
  79. console.log("open success");
  80. localStorage.removeItem("lockReconnect");
  81. //定时发送
  82. timer = window.setInterval(function () {
  83. ws.send("hello")
  84. }, 5000)
  85. }
  86. var $nowWait = $("#nowWait");
  87. ws.onmessage = function (e) {
  88. var res = JSON.parse(e.data);
  89. console.log(res)
  90. var len = res.data?res.data.length:0;
  91. if (len >= 0) {
  92. $nowWait.html(len);
  93. }
  94. }
  95. ws.onclose = function () {
  96. console.log("closed...")
  97. window.clearInterval(timer)
  98. reconnect(service)
  99. }
  100. ws.onerror = function (err) {
  101. window.clearInterval(timer)
  102. reconnect(service);
  103. };
  104. return ws
  105. }
  106. // 重连
  107. function reconnect(service) {
  108. // lockReconnect加锁,防止onclose、onerror两次重连
  109. if (limitConnect > 0) {
  110. if (localStorage.getItem('lockReconnect') != true) {
  111. localStorage.setItem("lockReconnect", 1);
  112. limitConnect--;
  113. timeConnect++;
  114. console.log("第" + timeConnect + "次重连");
  115. // 进行重连
  116. setTimeout(function () {
  117. webSocketInit(service);
  118. localStorage.removeItem("lockReconnect");
  119. }, 2000);
  120. }
  121. } else {
  122. console.log("TCP连接已超时");
  123. }
  124. }
  125. // 初始化ws
  126. var ws = webSocketInit(wsWaitUrl)
  127. // 开始或结束游戏
  128. var flag=true;
  129. function setStatus(num) {
  130. if(flag){
  131. flag=false;
  132. $.post(startGameUrl, JSON.stringify({ action: num }),function(){
  133. flag=true;
  134. });
  135. }
  136. }
  137. </script>
  138. </body>
  139. </html>