ws2.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script>
  7. var url = "ws://ws.anquanzhuli.com/ws?encoding=text&cId=1&gId=641&path=riskPointCount-back&from=aqpt.anquanzhuli.com";
  8. var ws = new WebSocket(url);
  9. ws.onopen = function (event) {
  10. console.log("Connection open ...");
  11. ws.send("riskPointCount-back");
  12. }
  13. ws.onmessage = function (p1) {
  14. console.log(p1.data);
  15. appendHtml(document.getElementById('container'), '<li>' + p1.data + '</li>');
  16. }
  17. function send() {
  18. ws.send(document.getElementById('txtInput').value);
  19. }
  20. function appendHtml(elem, value) {
  21. var node = document.createElement("div"),
  22. fragment = document.createDocumentFragment(),
  23. childs = null,
  24. i = 0;
  25. node.innerHTML = value;
  26. childs = node.childNodes;
  27. for (; i < childs.length; i++) {
  28. fragment.appendChild(childs[i]);
  29. }
  30. elem.appendChild(fragment);
  31. childs = null;
  32. fragment = null;
  33. node = null;
  34. }
  35. </script>
  36. </head>
  37. <body>
  38. <div>
  39. <ul id="container">
  40. </ul>
  41. <input type="text" id="txtInput"/>
  42. <input type="button" value="发言" onclick="send()">
  43. <a target="_blank" href="http://localhost:8899/push/msg?msg=hello from push api">使用API推送消息</a>
  44. </div>
  45. </body>
  46. </html>