Skip to content

Instantly share code, notes, and snippets.

@SeavantUUz
Created May 11, 2015 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeavantUUz/61fccd4c1723146e490b to your computer and use it in GitHub Desktop.
Save SeavantUUz/61fccd4c1723146e490b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head>
<script type="text/javascript">
var evtSrc = new EventSource("/test");
evtSrc.onmessage = function(e) {
console.log(e.data);
};
function send(){
var xhr = new XMLHttpRequest();
xhr.open("get", "/test", true);
xhr.send(null);
};
</script>
</head>
<body>
<h1>Server sent events</h1>
<div id="event"></div>
<button onclick="send()">发送消息</button>
</body>
</html>
@shellexy
Copy link

去改用 xhr.onreadystatechange()
注意 xhr.readyState === 3
然后 newText = xhr.responseText.substr(上次文本长度)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment