/** * H5 EventSource 实现web页面推送功能demo */ var serverData,statusDiv; var SERVER_URL = "index.php"; //servers Url Adress window.onload = function(){ /* <div id="statusDiv"></div> <div id="serverData"></div> */ serverData = document.getElementById("serverData"); statusDiv = document.getElementById("statusDiv"); startlistenServer(); } function startlistenServer(){ statusDiv.innerHTML = "start Connect Server..."; var es = new EventSource(SERVER_URL); es.addEventListener("newDate",newDateHandler); es.onopen =openHandler; es.onerror = errorHandler; es.onmessage = messageHandler; } function openHandler(e){ statusDiv.innerHTML="Server open"; } function errorHandler(e){ statusDiv.innerHTML="Error"; } function messageHandler(e){ serverData.innerHTML = e.data; } function newDateHandler(e){ serverData.innerHTML = e.data; }