rank.html 4.3 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 wsWaitUrl = "ws://" + baseUrl + "/ws/quepass/onlinewaiting";
  81. var wsCounter = "ws://" + baseUrl + "/ws/quepass/onlinecounter";
  82. var startGameUrl="http://"+baseUrl+"/api/quepass/status";
  83. var ansObj = {0:"A",1:"B",2:"C","-1":"超时未答"}
  84. var round_id = getQueryString("round_id")
  85. $("#round").text(round_id)
  86. function getQueryString(name) {
  87. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  88. var r = window.location.search.substr(1).match(reg);
  89. if (r != null) return unescape(r[2]); return null;
  90. }
  91. function webSocketInit(service) {
  92. var ws = new WebSocket(service);
  93. var timer = null
  94. ws.onopen = function () {
  95. console.log("open success");
  96. localStorage.removeItem("lockReconnect");
  97. var roundId = getQueryString("round_id")
  98. ws.send("roundId_"+roundId)
  99. // //定时发送
  100. // timer = window.setInterval(function () {
  101. // ws.send("roundId_"+roundId)
  102. // }, 1000)
  103. }
  104. ws.onmessage = function (e) {
  105. var res = JSON.parse(e.data);
  106. var len = res.length;
  107. $("#nowWait").text(len)
  108. var tr = "<tr><th>id</th><th>姓名</th><th>答对题目数量</th><th>总分</th><th>总耗时</th><th>排名</th></tr>"
  109. for(var i=0;i<len;i++){
  110. rank = i+1
  111. var td = "<tr><td>"+res[i].uid+"</td>"
  112. td += "<td>"+res[i].uname+"</td>"
  113. td += "<td>"+res[i].num+"</td>"
  114. td += "<td>"+res[i].total+"</td>"
  115. td += "<td>"+res[i].total_time+"</td>"
  116. td += "<td>"+rank+"</td>"
  117. tr += td
  118. }
  119. if(len){
  120. $("#rankTbl").html(tr)
  121. }
  122. }
  123. ws.onclose = function () {
  124. console.log("closed...")
  125. window.clearInterval(timer)
  126. reconnect(service)
  127. }
  128. ws.onerror = function (err) {
  129. window.clearInterval(timer)
  130. reconnect(service);
  131. };
  132. return ws
  133. }
  134. // 重连
  135. function reconnect(service) {
  136. // lockReconnect加锁,防止onclose、onerror两次重连
  137. if (limitConnect > 0) {
  138. if (localStorage.getItem('lockReconnect') != true) {
  139. localStorage.setItem("lockReconnect", 1);
  140. limitConnect--;
  141. timeConnect++;
  142. console.log("第" + timeConnect + "次重连");
  143. // 进行重连
  144. setTimeout(function () {
  145. webSocketInit(service);
  146. localStorage.removeItem("lockReconnect");
  147. }, 2000);
  148. }
  149. } else {
  150. console.log("TCP连接已超时");
  151. }
  152. }
  153. // 初始化ws
  154. var ws = webSocketInit(wsCounter)
  155. // 清空答题记录
  156. function setStatus(num) {
  157. if(window.confirm("你确认要清除当前轮的答题记录?清除后游戏状态也会重置,需要重新开始游戏!"))
  158. $.post(startGameUrl, JSON.stringify({ action: num,roundId:round_id }),function(){
  159. flag=true;
  160. });
  161. }
  162. </script>
  163. </body>
  164. </html>