testwebsocket-queue-100715.html 3.6 KB

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