test.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. margin-top: 20px;
  46. display: flex;
  47. justify-content: space-around;
  48. }
  49. .btn-box a {
  50. display: block;
  51. }
  52. h3{
  53. /* color: #fff; */
  54. text-align: center;
  55. }
  56. table th,td{
  57. /* color: #fff; */
  58. text-align: center;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <div class="full">
  64. <!-- <img src="img/ptop.png" class="p-top" alt=""> -->
  65. <div class="btn-box">
  66. <a href="/rank.html?round_id=1" class="btn1"><button>第一轮</button></a>
  67. <a href="/rank.html?round_id=2" class="btn2"><button>第二轮</button></a>
  68. <a href="/rank.html?round_id=3" class="btn2"><button>第三轮</button></a>
  69. </div>
  70. <p></p>
  71. <h3>第<span id="round"></span>轮(当前答题人数:<span id="nowWait"></span>) <button onclick="setStatus(999)">清空答题记录</button></h3>
  72. <table border="1" cellspacing="0" cellpadding="0" width="70%" align="center" id="rankTbl">
  73. </table>
  74. </div>
  75. <script src="js/jquery.js"></script>
  76. <script>
  77. var limitConnect = 3; // 断线重连次数
  78. var timeConnect = 0;
  79. var baseUrl = "gj.100t.com";
  80. var registUrl = "http://" + baseUrl + "/api/quepass/regist";
  81. var wsWaitUrl = "ws://" + baseUrl + "/ws/quepass/onlinewaiting";
  82. var wsCounter = "ws://" + baseUrl + "/ws/quepass/onlinecounter";
  83. var startGameUrl="http://"+baseUrl+"/api/quepass/status";
  84. var ansObj = {0:"A",1:"B",2:"C","-1":"超时未答"}
  85. var round_id = getQueryString("round_id")
  86. $("#round").text(round_id)
  87. function getQueryString(name) {
  88. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  89. var r = window.location.search.substr(1).match(reg);
  90. if (r != null) return unescape(r[2]); return null;
  91. }
  92. function webSocketInit(service,uid,uname) {
  93. $.post(registUrl,JSON.stringify({uid,uname}),function(res){
  94. })
  95. var ws = new WebSocket(service);
  96. var timer = null
  97. ws.onopen = function () {
  98. console.log("open success");
  99. localStorage.removeItem("lockReconnect");
  100. var roundId = getQueryString("round_id")
  101. //定时发送
  102. timer = window.setInterval(function () {
  103. console.log(uid,uname)
  104. ws.send(uid+"_"+uname)
  105. }, 2000)
  106. }
  107. ws.onmessage = function (e) {
  108. var res = JSON.parse(e.data);
  109. var len = res.length;
  110. $("#nowWait").text(len)
  111. var tr = "<tr><th>id</th><th>姓名</th><th>答对题目数量</th><th>总分</th><th>总耗时</th><th>排名</th></tr>"
  112. for(var i=0;i<len;i++){
  113. rank = i+1
  114. var td = "<tr><td>"+res[i].uid+"</td>"
  115. td += "<td>"+res[i].uname+"</td>"
  116. td += "<td>"+res[i].num+"</td>"
  117. td += "<td>"+res[i].total+"</td>"
  118. td += "<td>"+res[i].total_time+"</td>"
  119. td += "<td>"+rank+"</td>"
  120. tr += td
  121. }
  122. if(len){
  123. $("#rankTbl").html(tr)
  124. }
  125. }
  126. ws.onclose = function () {
  127. console.log("closed...")
  128. window.clearInterval(timer)
  129. reconnect(service)
  130. }
  131. ws.onerror = function (err) {
  132. window.clearInterval(timer)
  133. reconnect(service);
  134. };
  135. return ws
  136. }
  137. // 重连
  138. function reconnect(service) {
  139. // lockReconnect加锁,防止onclose、onerror两次重连
  140. if (limitConnect > 0) {
  141. if (localStorage.getItem('lockReconnect') != true) {
  142. localStorage.setItem("lockReconnect", 1);
  143. limitConnect--;
  144. timeConnect++;
  145. console.log("第" + timeConnect + "次重连");
  146. // 进行重连
  147. setTimeout(function () {
  148. webSocketInit(service);
  149. localStorage.removeItem("lockReconnect");
  150. }, 2000);
  151. }
  152. } else {
  153. console.log("TCP连接已超时");
  154. }
  155. }
  156. // 初始化ws
  157. for(var i=0;i<100;i++){
  158. var ws = webSocketInit(wsCounter,i,"test"+i)
  159. }
  160. </script>
  161. </body>
  162. </html>