亲测通过,可以实时将消息发送至客户端,几乎零延时。。。,很快
不像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
SSE浏览器 兼容性如下:
资源文件列表
.nuget/packages.config , 302
aspnetwebapi-html5push.sln , 1275
aspnetwebapi-html5push.suo , 51712
aspnetwebapi-html5push/aspnetwebapi-html5push.csproj , 14386
aspnetwebapi-html5push/aspnetwebapi-html5push.csproj.user , 1168
aspnetwebapi-html5push/bin/EntityFramework.dll , 785184
aspnetwebapi-html5push/bin/EntityFramework.xml , 821283
aspnetwebapi-html5push/bin/Microsoft.Web.Infrastructure.dll , 45416
aspnetwebapi-html5push/bin/MvcApplication2-4.dll , 10240
aspnetwebapi-html5push/bin/MvcApplication2-4.dll.config , 4009
aspnetwebapi-html5push/bin/MvcApplication2-4.pdb , 19968
aspnetwebapi-html5push/bin/Newtonsoft.Json.dll , 374272
aspnetwebapi-html5push/bin/Newtonsoft.Json.pdb , 1025536
aspnetwebapi-html5push/bin/Newtonsoft.Json.xml , 406212
aspnetwebapi-html5push/bin/System.Json.dll , 66408
aspnetwebapi-html5push/bin/System.Json.xml , 59669
aspnetwebapi-html5push/bin/System.Net.Http.dll , 181856
aspnetwebapi-html5push/bin/System.Net.Http.Formatting.dll , 165472
aspnetwebapi-html5push/bin/System.Net.Http.Formatting.xml , 116511
aspnetwebapi-html5push/bin/System.Net.Http.WebRequest.dll , 15456
aspnetwebapi-html5push/bin/System.Net.Http.WebRequest.xml , 5011
aspnetwebapi-html5push/bin/System.Net.Http.xml , 153934
aspnetwebapi-html5push/bin/System.Web.Helpers.dll , 138352
aspnetwebapi-html5push/bin/System.Web.Helpers.xml , 70262
aspnetwebapi-html5push/bin/System.Web.Http.dll , 364128
aspnetwebapi-html5push/bin/System.Web.Http.WebHost.dll , 66144
aspnetwebapi-html5push/bin/System.Web.Http.WebHost.xml , 7003
aspnetwebapi-html5push/bin/System.Web.Http.xml , 170786
aspnetwebapi-html5push/bin/System.Web.Mvc.dll , 497248
aspnetwebapi-html5push/bin/System.Web.Mvc.xml , 758931
aspnetwebapi-html5push/bin/System.Web.Optimization.dll , 311616
aspnetwebapi-html5push/bin/System.Web.Providers.dll , 269120
aspnetwebapi-html5push/bin/System.Web.Razor.dll , 264816
aspnetwebapi-html5push/bin/System.Web.Razor.xml , 357928
aspnetwebapi-html5push/bin/System.Web.WebPages.Deployment.dll , 40560
aspnetwebapi-html5push/bin/System.Web.WebPages.Deployment.xml , 3586
aspnetwebapi-html5push/bin/System.Web.WebPages.dll , 204400
aspnetwebapi-html5push/bin/System.Web.WebPages.Razor.dll , 39536
aspnetwebapi-html5push/bin/System.Web.WebPages.Razor.xml , 16239
aspnetwebapi-html5push/bin/System.Web.WebPages.xml , 197549
aspnetwebapi-html5push/Content/Site.css , 11241
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_flat_0_aaaaaa_40x100.png , 180
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_flat_75_ffffff_40x100.png , 178
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_55_fbf9ee_1x400.png , 120
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_65_ffffff_1x400.png , 105
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_75_dadada_1x400.png , 111
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_75_e6e6e6_1x400.png , 110
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_95_fef1ec_1x400.png , 119
aspnetwebapi-html5push/Content/themes/base/images/ui-bg_highlight-soft_75_cccccc_1x100.png , 101
aspnetwebapi-html5push/Content/themes/base/images/ui-icons_222222_256x240.png , 4369
aspnetwebapi-html5push/Content/themes/base/images/ui-icons_2e83ff_256x240.png , 4369
aspnetwebapi-html5push/Content/themes/base/images/ui-icons_454545_256x240.png , 4369
aspnetwebapi-html5push/Content/themes/base/images/ui-icons_888888_256x240.png , 4369
aspnetwebapi-html5push/Content/themes/base/images/ui-icons_cd0a0a_256x240.png , 4369
aspnetwebapi-html5push/Content/themes/base/jquery.ui.accordion.css , 1391
aspnetwebapi-html5push/Content/themes/base/jquery.ui.all.css , 616
aspnetwebapi-html5push/Content/themes/base/jquery.ui.autocomplete.css , 1755
aspnetwebapi-html5push/Content/themes/base/jquery.ui.base.css , 427
aspnetwebapi-html5push/Content/themes/base/jquery.ui.button.css , 2796
aspnetwebapi-html5push/Content/themes/base/jquery.ui.core.css , 1784
aspnetwebapi-html5push/Content/themes/base/jquery.ui.datepicker.css , 4371
aspnetwebapi-html5push/Content/themes/base/jquery.ui.dialog.css , 1683
aspnetwebapi-html5push/Content/themes/base/jquery.ui.progressbar.css , 682
aspnetwebapi-html5push/Content/themes/base/jquery.ui.resizable.css , 1497
aspnetwebapi-html5push/Content/themes/base/jquery.ui.selectable.css , 648
aspnetwebapi-html5push/Content/themes/base/jquery.ui.slider.css , 1465
aspnetwebapi-html5push/Content/themes/base/jquery.ui.tabs.css , 1708
aspnetwebapi-html5push/Content/themes/base/jquery.ui.theme.css , 19467
aspnetwebapi-html5push/Controllers/ChatController.cs , 4046
aspnetwebapi-html5push/Controllers/HomeController.cs , 298
aspnetwebapi-html5push/Global.asax , 113
aspnetwebapi-html5push/Global.asax.cs , 1439
aspnetwebapi-html5push/Images/accent.png , 528
aspnetwebapi-html5push/Images/aspNetHome.png , 3736
aspnetwebapi-html5push/Images/bullet.png , 2867
aspnetwebapi-html5push/Images/facebook.png , 529
aspnetwebapi-html5push/Images/findHosting.png , 4329
aspnetwebapi-html5push/Images/heroAccent.png , 572
aspnetwebapi-html5push/Images/NuGetGallery.png , 4144
aspnetwebapi-html5push/Images/orderedListOne.png , 653
aspnetwebapi-html5push/Images/orderedListThree.png , 772
aspnetwebapi-html5push/Images/orderedListTwo.png , 733
aspnetwebapi-html5push/Images/twitter.png , 488
aspnetwebapi-html5push/Images/windowsLive.png , 3302
aspnetwebapi-html5push/Models/Message.cs , 291
aspnetwebapi-html5push/obj/Debug/aspnetwebapi-html5push.csproj.FileListAbsolute.txt , 14819
aspnetwebapi-html5push/obj/Debug/aspnetwebapi-html5push.csprojResolveAssemblyReference.cache , 137762
aspnetwebapi-html5push/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 21758
aspnetwebapi-html5push/obj/Debug/MvcApplication2-4.csproj.FileListAbsolute.txt , 4286
aspnetwebapi-html5push/obj/Debug/MvcApplication2-4.dll , 10240
aspnetwebapi-html5push/obj/Debug/MvcApplication2-4.pdb , 19968
aspnetwebapi-html5push/obj/Debug/ResolveAssemblyReference.cache , 357126
aspnetwebapi-html5push/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs , 0
aspnetwebapi-html5push/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs , 0
aspnetwebapi-html5push/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs , 0
aspnetwebapi-html5push/packages.config , 2540
aspnetwebapi-html5push/Properties/AssemblyInfo.cs , 1370
aspnetwebapi-html5push/Scripts/jquery-1.6.2-vsdoc.js , 337855
aspnetwebapi-html5push/Scripts/jquery-1.6.2.js , 237682
aspnetwebapi-html5push/Scripts/jquery-1.6.2.min.js , 93128
aspnetwebapi-html5push/Scripts/jquery-ui-1.8.11.js , 421555
aspnetwebapi-html5push/Scripts/jquery-ui-1.8.11.min.js , 259843
aspnetwebapi-html5push/Scripts/jquery.unobtrusive-ajax.js , 5605
aspnetwebapi-html5push/Scripts/jquery.unobtrusive-ajax.min.js , 2745
aspnetwebapi-html5push/Scripts/jquery.validate-vsdoc.js , 44222
aspnetwebapi-html5push/Scripts/jquery.validate.js , 38932
aspnetwebapi-html5push/Scripts/jquery.validate.min.js , 22645
aspnetwebapi-html5push/Scripts/jquery.validate.unobtrusive.js , 15387
aspnetwebapi-html5push/Scripts/jquery.validate.unobtrusive.min.js , 4414
aspnetwebapi-html5push/Scripts/knockout.debug.js , 158803
aspnetwebapi-html5push/Scripts/knockout.js , 39817
aspnetwebapi-html5push/Scripts/knockout.mapping-latest.js , 8037
aspnetwebapi-html5push/Scripts/modernizr-2.0.6-development-only.js , 45718
aspnetwebapi-html5push/Scripts/_references.js , 242
aspnetwebapi-html5push/StyleCop.Cache , 11062
aspnetwebapi-html5push/Views/Home/Index.cshtml , 4431
aspnetwebapi-html5push/Views/Shared/Error.cshtml , 254
aspnetwebapi-html5push/Views/Shared/_Layout.cshtml , 582
aspnetwebapi-html5push/Views/Web.config , 2815
aspnetwebapi-html5push/Views/_ViewStart.cshtml , 53
aspnetwebapi-html5push/Web.config , 4009
aspnetwebapi-html5push/Web.Debug.config , 1256
aspnetwebapi-html5push/Web.Release.config , 1316
Backup/aspnetwebapi-html5push.sln , 1173
Backup/aspnetwebapi-html5push/aspnetwebapi-html5push.csproj , 13040
Backup/aspnetwebapi-html5push/aspnetwebapi-html5push.csproj.user , 1060
Backup/aspnetwebapi-html5push/Content/Site.css , 11241
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_flat_0_aaaaaa_40x100.png , 180
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_flat_75_ffffff_40x100.png , 178
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_55_fbf9ee_1x400.png , 120
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_65_ffffff_1x400.png , 105
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_75_dadada_1x400.png , 111
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_75_e6e6e6_1x400.png , 110
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_glass_95_fef1ec_1x400.png , 119
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-bg_highlight-soft_75_cccccc_1x100.png , 101
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-icons_222222_256x240.png , 4369
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-icons_2e83ff_256x240.png , 4369
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-icons_454545_256x240.png , 4369
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-icons_888888_256x240.png , 4369
Backup/aspnetwebapi-html5push/Content/themes/base/images/ui-icons_cd0a0a_256x240.png , 4369
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.accordion.css , 1391
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.all.css , 616
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.autocomplete.css , 1755
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.base.css , 427
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.button.css , 2796
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.core.css , 1784
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.datepicker.css , 4371
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.dialog.css , 1683
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.progressbar.css , 682
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.resizable.css , 1497
Backup/aspnetwebapi-html5push/Content/themes/base/jquery.ui.selectable.css , 648
最多只能显示150条信息!
