testwebsocket-100715.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>100715</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. <script>
  22. var socket = new WebSocket("ws://39.105.187.28:3006");
  23. socket.onopen = function(event) {
  24. console.log("connect suc");
  25. // 监听消息
  26. socket.onmessage = function(event) {
  27. console.log('Client received a message '+event.data);
  28. var msg = JSON.parse(event.data);
  29. };
  30. // 监听Socket的关闭
  31. socket.onclose = function(event) {
  32. console.log('Client notified socket has closed');
  33. };
  34. // 关闭Socket....
  35. //socket.close()
  36. };
  37. //测试登录im接口
  38. function login() {
  39. var data = {};
  40. data['id'] = 1;
  41. data['route'] = 'login';
  42. var req_data = {};
  43. req_data.page_name = 3;
  44. req_data.uid = document.getElementById("uid").value;
  45. req_data.ticket = document.getElementById("ticket").value;
  46. data['req_data'] = req_data;
  47. socket.send(JSON.stringify(data));
  48. }
  49. function enterChatRoom() {
  50. socket.send(JSON.stringify({
  51. "id":1,
  52. "route":"enterChatRoom",
  53. "req_data":{
  54. "room_id":document.getElementById("roomId").value
  55. }
  56. }));
  57. }
  58. function sendMessage() {
  59. socket.send(JSON.stringify({
  60. "id":1,
  61. "route":"sendMessage",
  62. "req_data":{
  63. "room_id":document.getElementById("roomId").value,
  64. "content":document.getElementById("message").value,
  65. }
  66. }));
  67. }
  68. function heartbeat() {
  69. var data = {};
  70. data['id'] = 1;
  71. data['route'] = 'heartbeat';
  72. var req_data = {};
  73. req_data.page_name = 3;
  74. //data['req_data'] = req_data;
  75. var str = JSON.stringify(data);
  76. socket.send(str);
  77. }
  78. setInterval(function() {heartbeat();}, 5000);
  79. </script>
  80. </body>
  81. </html>
  82.