HTML5 服务器发送事件(server-sent event)允许网页获得来自服务器的更新
JS端 实例化EventSource对象,并且实现监听
$("#getFile").click(function () { //监听IP+URLP 推送的消息 var source = new EventSource(IP + URLP); source.onmessage = function (event) { alert(event.data); //如果不close(),每隔3-5s会再次收到推送 source.close(); }; });
服务端
context.Response.ContentType = "text/event-stream"; context.Response.CacheControl = "no-cache" context.Response.Write("data:" + fileId + "\n\n"); // \n\n 就相当于 context.Response.Flush();