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