testwebsocket.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <div>
  9. </div>
  10. <input id="uid" type="text" value="100691" placeholder="uid"/> <br/>
  11. <input id="ticket" type="text"
  12. value="eyJhbGciOiJIUzI1NiJ9.eyJ0aWNrZXRfdHlwZSI6bnVsbCwidWlkIjoxMDA2OTEsInRpY2tldF9pZCI6IjIzNzBiZjk3LTQ3MzQtNDY0OC1iMjM3LTExMWRiMTk3NTliZiIsImV4cCI6MzYwMCwiY2xpZW50X2lkIjoiZXJiYW4tY2xpZW50In0.s3S5WKIFRFaLIjpUwmS_VjBNg0RvQnQ_rD6oFxPVroQ"
  13. placeholder="ticket"/> <br/>
  14. <input type="button" value="login" onclick="login()"/><br/>
  15. <hr>
  16. <input id="publicRoomId" type="text" value="1"/><br/>
  17. <input type="button" value="进入大厅" onclick="enterPublicRoom()"/><br/>
  18. <hr>
  19. <input id="publicRoomMsg" type="text" value=""/><br/>
  20. <input type="button" value="大厅发送消息" onclick="sendPublicMsg()"/><br/>
  21. <hr>
  22. <input id="roomId" type="text" value="" placeholder="roomId"/> <br/>
  23. <input type="button" value="进入房间" onclick="enterChatRoom()"/><br/>
  24. <hr>
  25. <input id="message" type="text" value="" placeholder="message"/> <br/>
  26. <input type="button" value="发送文本消息" onclick="sendMessage()"/><br/>
  27. <hr>
  28. <hr>
  29. <input type="button" value="获取房间队列" onclick="fetchQueue()"/><br/>
  30. <hr>
  31. <hr>
  32. <input id="updateKey" type="text" value="" placeholder="updateKey"/> <br/>
  33. <input id="updateUid" type="text" value="" placeholder="updateUid"/> <br/>
  34. <input type="button" value="更新房间队列" onclick="updateQueue()"/><br/>
  35. <hr>
  36. <hr>
  37. <input id="pollKey" type="text" value="" placeholder="pollKey"/> <br/>
  38. <input type="button" value="取出房间队列" onclick="pollQueue()"/><br/>
  39. <hr>
  40. <hr>
  41. <script>
  42. var socket = new WebSocket("ws://39.105.187.28:3006");
  43. socket.onopen = function (event) {
  44. console.log("connect suc");
  45. // 监听消息
  46. socket.onmessage = function (event) {
  47. var msg = JSON.parse(event.data);
  48. if ("heartbeat" == msg.route) {
  49. return;
  50. }
  51. console.log('Client received a message ' + event.data);
  52. };
  53. // 监听Socket的关闭
  54. socket.onclose = function (event) {
  55. console.log('Client notified socket has closed');
  56. };
  57. // 关闭Socket....
  58. //socket.close()
  59. };
  60. //测试登录im接口
  61. function login() {
  62. var data = {};
  63. data['id'] = 1;
  64. data['route'] = 'login';
  65. var req_data = {};
  66. req_data.page_name = 3;
  67. req_data.uid = document.getElementById("uid").value;
  68. req_data.ticket = document.getElementById("ticket").value;
  69. data['req_data'] = req_data;
  70. socket.send(JSON.stringify(data));
  71. }
  72. function enterChatRoom() {
  73. socket.send(JSON.stringify({
  74. "id": 1,
  75. "route": "enterChatRoom",
  76. "req_data": {
  77. "room_id": document.getElementById("roomId").value
  78. }
  79. }));
  80. }
  81. function sendMessage() {
  82. socket.send(JSON.stringify({
  83. "id": 1,
  84. "route": "sendMessage",
  85. "req_data": {
  86. "room_id": document.getElementById("roomId").value,
  87. "content": document.getElementById("message").value,
  88. }
  89. }));
  90. }
  91. function fetchQueue() {
  92. socket.send(JSON.stringify({
  93. "id": 1,
  94. "route": "fetchQueue",
  95. "req_data": {
  96. "room_id": document.getElementById("roomId").value,
  97. }
  98. }));
  99. }
  100. function updateQueue() {
  101. socket.send(JSON.stringify({
  102. "id": 1,
  103. "route": "updateQueue",
  104. "req_data": {
  105. "room_id": document.getElementById("roomId").value,
  106. "key": document.getElementById("updateKey").value,
  107. "uid": document.getElementById("updateUid").value,
  108. }
  109. }));
  110. }
  111. function pollQueue() {
  112. socket.send(JSON.stringify({
  113. "id": 1,
  114. "route": "pollQueue",
  115. "req_data": {
  116. "room_id": document.getElementById("roomId").value,
  117. "key": document.getElementById("pollKey").value,
  118. }
  119. }));
  120. }
  121. function heartbeat() {
  122. var data = {};
  123. data['id'] = 1;
  124. data['route'] = 'heartbeat';
  125. var req_data = {};
  126. req_data.page_name = 3;
  127. //data['req_data'] = req_data;
  128. var str = JSON.stringify(data);
  129. socket.send(str);
  130. }
  131. function enterPublicRoom() {
  132. socket.send(JSON.stringify({
  133. "id": 1,
  134. "route": "enterPublicRoom",
  135. "req_data": {
  136. "room_id": document.getElementById("publicRoomId").value
  137. }
  138. }));
  139. }
  140. function sendPublicMsg() {
  141. console.log("======================");
  142. socket.send(JSON.stringify({
  143. "id": 1,
  144. "route": "sendPublicMsg",
  145. "req_data": {
  146. "room_id": document.getElementById("publicRoomId").value,
  147. "custom": document.getElementById("publicRoomMsg").value
  148. }
  149. }));
  150. }
  151. setInterval(function () {
  152. heartbeat();
  153. }, 5000);
  154. </script>
  155. </body>
  156. </html>
  157.