EventSource.html 1005 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>EventSource客户端</title>
  6. <script type="text/javascript">
  7. function EventSourceTest(url)
  8. {
  9. if (typeof(EventSource) !== "undefined")
  10. {
  11. var es = new EventSource(url);
  12. es.onopen = function()
  13. {
  14. alert("连接已建立");
  15. };
  16. es.onmessage = function(ev)
  17. {
  18. console.log("received event: " + ev.data);
  19. var li=document.createElement("li");
  20. li.innerHTML=ev.data;
  21. document.getElementById("msg_list").appendChild(li);
  22. };
  23. es.onerror = function(e)
  24. {
  25. alert("连接断开");
  26. };
  27. }
  28. else
  29. {
  30. alert("您的浏览器不支持 EventSource!");
  31. }
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. URL: <input type="text" id="url" value="http://127.0.0.1:8080/sse" style="width:300px;">
  37. <button onclick="EventSourceTest(document.getElementById('url').value)">运行 EventSource</button>
  38. <div>
  39. <ul id="msg_list" style="height:500px;overflow-y:scroll;">
  40. </ul>
  41. <div>
  42. </body>
  43. </html>