码迷,mamicode.com
首页 > 其他好文 > 详细

服务器推送之SSE简单使用

时间:2019-12-06 13:43:38      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:sage   serve   exp   ice   viewport   eof   name   fine   oid   

前端

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <h1>获得服务器更新</h1>
    <div id="result"></div>
    <script>
        if (typeof (EventSource) !== "undefined") {
            var source = new EventSource("/HTML5/tt");
            source.onmessage = function (event) {
                document.getElementById("result").innerHTML += event.data + "<br />";
            };
        }
        else {
            document.getElementById("result").innerHTML = "抱歉,您的浏览器不支持 server-sent 事件 ...";
        }
    </script>
</body>
</html>

后端

public class HTML5Controller : Controller
    {
        //
        // GET: /HTML5/

        public ActionResult Index()
        {
            return View();
        }

        public void tt()
        {
            Response.ContentType = "text/event-stream";
            Response.Expires = -1;
            Response.Write("data: " + DateTime.Now.ToString() + "\n\n");//注意结束要两个\n
            Response.Flush();
        }
    }

 

服务器推送之SSE简单使用

标签:sage   serve   exp   ice   viewport   eof   name   fine   oid   

原文地址:https://www.cnblogs.com/Cengjianwei/p/11994520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!