資源簡介
親測通過,可以實時將消息發送至客戶端,幾乎零延時。。。,很快
不像ajax一樣,一直刷。。。
最近在項目開發中使用了一種比較新的技術,SSE,即 Server-Send Event,使用它從服務器向瀏覽器推送數據。
傳統的網頁都是瀏覽器向服務器“查詢”數據,但是很多場合,最有效的方式是服務器向瀏覽器“發送”數據。比如,每當收到新的電子郵件,服務器就向瀏覽器發送一個“通知”,這要比瀏覽器按時向服務器查詢(polling)更有效率。
服務器發送事件(Server-Sent Events,簡稱SSE)就是為了解決這個問題,而提出的一種新API,部署在EventSource對象上。目前,除了IE,其他主流瀏覽器都支持。
簡單說,所謂SSE,就是瀏覽器向服務器發送一個HTTP請求,然后服務器不斷單向地向瀏覽器推送“信息”(message)。這種信息在格式上很簡單,就是“信息”加上前綴“data: ”,然后以“\n\n”結尾。
SSE與WebSocket有相似功能,都是用來建立瀏覽器與服務器之間的通信渠道。兩者的區別在于:
WebSocket是全雙工通道,可以雙向通信,功能更強;SSE是單向通道,只能服務器向瀏覽器端發送。
WebSocket是一個新的協議,需要服務器端支持;SSE則是部署在HTTP協議之上的,現有的服務器軟件都支持。
SSE是一個輕量級協議,相對簡單;WebSocket是一種較重的協議,相對復雜。
SSE默認支持斷線重連,WebSocket則需要額外部署。
SSE支持自定義發送的數據類型。
從上面的比較可以看出,兩者各有特點,適合不同的場合。
提示:除了IE之外,幾乎所有瀏覽器均支持 SSE
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.Http;
using?System.Web.Mvc;
using?System.Web.Optimization;
using?System.Web.Routing;
namespace?Strathweb.HTML5push
{
????//?Note:?For?instructions?on?enabling?IIS6?or?IIS7?classic?mode?
????//?visit?http://go.microsoft.com/?linkId=9394801
????public?class?WebApiApplication?:?System.Web.HttpApplication
????{
????????public?static?void?RegisterGlobalFilters(GlobalFilterCollection?filters)
????????{
????????????filters.Add(new?HandleErrorAttribute());
????????}
????????public?static?void?RegisterRoutes(RouteCollection?routes)
????????{
????????????routes.IgnoreRoute(“{resource}.axd/{*pathInfo}“);
????????????routes.MapHttpRoute(
????????????????name:?“DefaultApi“
????????????????routeTe
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2012-05-06?19:31??.nuget\
?????文件?????????302??2012-05-06?19:31??.nuget\packages.config
?????目錄???????????0??2016-12-31?23:16??aspnetwebapi-html5push\
?????文件????????1275??2016-12-31?20:12??aspnetwebapi-html5push.sln
?????文件???????51712??2012-05-06?19:31??aspnetwebapi-html5push.suo
?????目錄???????????0??2016-12-31?19:57??aspnetwebapi-html5push\App_Data\
?????文件???????14386??2016-12-31?23:16??aspnetwebapi-html5push\aspnetwebapi-html5push.csproj
?????文件????????1168??2016-12-31?23:16??aspnetwebapi-html5push\aspnetwebapi-html5push.csproj.user
?????目錄???????????0??2016-12-31?20:15??aspnetwebapi-html5push\bin\
?????文件??????785184??2012-05-06?19:31??aspnetwebapi-html5push\bin\Entityfr
?????文件??????821283??2012-05-06?19:31??aspnetwebapi-html5push\bin\Entityfr
?????文件???????45416??2012-05-06?19:31??aspnetwebapi-html5push\bin\Microsoft.Web.Infrastructure.dll
?????文件???????10240??2016-12-31?23:16??aspnetwebapi-html5push\bin\MvcApplication2-4.dll
?????文件????????4009??2016-12-31?23:16??aspnetwebapi-html5push\bin\MvcApplication2-4.dll.config
?????文件???????19968??2016-12-31?23:16??aspnetwebapi-html5push\bin\MvcApplication2-4.pdb
?????文件??????374272??2012-05-06?19:31??aspnetwebapi-html5push\bin\Newtonsoft.Json.dll
?????文件?????1025536??2012-05-06?19:31??aspnetwebapi-html5push\bin\Newtonsoft.Json.pdb
?????文件??????406212??2012-05-06?19:31??aspnetwebapi-html5push\bin\Newtonsoft.Json.xm
?????文件???????66408??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Json.dll
?????文件???????59669??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Json.xm
?????文件??????181856??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Net.Http.dll
?????文件??????165472??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Net.Http.Formatting.dll
?????文件??????116511??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Net.Http.Formatting.xm
?????文件???????15456??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Net.Http.WebRequest.dll
?????文件????????5011??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Net.Http.WebRequest.xm
?????文件??????153934??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Net.Http.xm
?????文件??????138352??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Web.Helpers.dll
?????文件???????70262??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Web.Helpers.xm
?????文件??????364128??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Web.Http.dll
?????文件???????66144??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Web.Http.WebHost.dll
?????文件????????7003??2012-05-06?19:31??aspnetwebapi-html5push\bin\System.Web.Http.WebHost.xm
............此處省略471個文件信息
評論
共有 條評論