ws_group.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1.  <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  5. <title></title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. var socket;
  10. var messageNum=0;
  11. if (!window.WebSocket) {
  12. window.WebSocket = window.MozWebSocket;
  13. }
  14. if (window.WebSocket) {
  15. // socket = new WebSocket("ws://127.0.0.1:8390?group_id=1");
  16. socket = new WebSocket("ws://test-ws.meechat.club?group_id=1");
  17. socket.onmessage = function(event) {
  18. messageNum = messageNum+1;
  19. var ta = document.getElementById('responseText');
  20. ta.value=messageNum
  21. };
  22. socket.onopen = function(event) {
  23. var ta = document.getElementById('responseText');
  24. ta.value = "连接开启!\n";
  25. };
  26. socket.onclose = function(event) {
  27. var ta = document.getElementById('responseText');
  28. ta.value = ta.value + "连接关闭\n";
  29. };
  30. } else {
  31. alert("您的浏览器不支持 WebSocket!");
  32. }
  33. function send(message) {
  34. if (!window.WebSocket) {
  35. return;
  36. }
  37. if (socket.readyState == WebSocket.OPEN) {
  38. socket.send(message);
  39. } else {
  40. alert("连接没有开启");
  41. }
  42. }
  43. </script>
  44. <form onsubmit="return false;">
  45. <h3></h3>
  46. <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
  47. <br>
  48. <input type="text" name="message" style="width: 300px" value="websocket连接工具">
  49. </form>
  50. <br>
  51. <br>
  52. </body>
  53. </html>