page.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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";
  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. }, 1000)
  85. }
  86. var $nowWait = $("#nowWait");
  87. ws.onmessage = function (e) {
  88. var res = JSON.parse(e.data);
  89. var len = res.data?res.data.length:0;
  90. if (len >= 0) {
  91. $nowWait.html(len);
  92. }
  93. }
  94. ws.onclose = function () {
  95. console.log("closed...")
  96. window.clearInterval(timer)
  97. reconnect(service)
  98. }
  99. ws.onerror = function (err) {
  100. window.clearInterval(timer)
  101. reconnect(service);
  102. };
  103. return ws
  104. }
  105. // 重连
  106. function reconnect(service) {
  107. // lockReconnect加锁,防止onclose、onerror两次重连
  108. if (limitConnect > 0) {
  109. if (localStorage.getItem('lockReconnect') != true) {
  110. localStorage.setItem("lockReconnect", 1);
  111. limitConnect--;
  112. timeConnect++;
  113. console.log("第" + timeConnect + "次重连");
  114. // 进行重连
  115. setTimeout(function () {
  116. webSocketInit(service);
  117. localStorage.removeItem("lockReconnect");
  118. }, 2000);
  119. }
  120. } else {
  121. console.log("TCP连接已超时");
  122. }
  123. }
  124. // 初始化ws
  125. var ws = webSocketInit(wsWaitUrl)
  126. // 开始或结束游戏
  127. var flag=true;
  128. function setStatus(num) {
  129. if(flag){
  130. flag=false;
  131. $.post(startGameUrl, JSON.stringify({ action: num }),function(){
  132. flag=true;
  133. });
  134. }
  135. }
  136. </script>
  137. </body>
  138. </html>