HTML5¼¼Êõ

HTML5Ö§³Ö·þÎñÆ÷·¢ËÍʼþ(Server-Sent Events)-µ¥ÏòÏûÏ¢´«µÝÊý¾ÝÍÆËÍ£¨C#ʾÀý£© - ÐÜ×ÐÆäÈË

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-05-19 11:02 ÎÒÒªÆÀÂÛ( )

´«Í³µÄWEBÓ¦ÓóÌÐòͨÐÅʱµÄ¼òµ¥Ê±Ðòͼ£º ÏÖÔÚWeb AppÖУ¬´ó¶¼ÓÐAjax£¬ÊÇÕâÑù×Ó£º HTML5ÓÐÒ»¸öServer-Sent Events(SSE)¹¦ÄÜ£¬ÔÊÐí·þÎñ¶ËÍÆËÍÊý¾Ýµ½¿Í»§¶Ë¡£(ͨ³£½Ð Êý¾ÝÍÆËÍ )¡£»ùÓÚÊý¾ÝÍÆËÍÊÇÕâÑùµÄ£¬µ±Êý¾ÝÔ´ÓÐÐÂÊý¾Ý£¬ËüÂíÉÏ·¢Ë͵½¿Í»§¶Ë£¬²»ÐèÒªµÈ´ý¿Í»§

´«Í³µÄWEBÓ¦ÓóÌÐòͨÐÅʱµÄ¼òµ¥Ê±Ðòͼ£º

ÏÖÔÚWeb AppÖУ¬´ó¶¼ÓÐAjax£¬ÊÇÕâÑù×Ó£º

 

HTML5ÓÐÒ»¸öServer-Sent Events(SSE)¹¦ÄÜ£¬ÔÊÐí·þÎñ¶ËÍÆËÍÊý¾Ýµ½¿Í»§¶Ë¡£(ͨ³£½ÐÊý¾ÝÍÆËÍ)¡£»ùÓÚÊý¾ÝÍÆËÍÊÇÕâÑùµÄ£¬µ±Êý¾ÝÔ´ÓÐÐÂÊý¾Ý£¬ËüÂíÉÏ·¢Ë͵½¿Í»§¶Ë£¬²»ÐèÒªµÈ´ý¿Í»§¶ËÇëÇó¡£ÕâЩÐÂÊý¾Ý¿ÉÄÜÊÇ×îÐÂÎÅ£¬×îйÉƱÐÐÇ飬À´×ÔÅóÓѵÄÁÄÌìÐÅÏ¢£¬ÌìÆøÔ¤±¨µÈ¡£

Êý¾ÝÀ­ÓëÍƵŦÄÜÊÇÒ»ÑùµÄ£¬Óû§Äõ½ÐÂÊý¾Ý¡£µ«Êý¾ÝÍÆËÍÓÐһЩÓÅÊÆ¡£ Äã¿ÉÄÜÌý˵¹ýComet, AjaxÍÆËÍ, ·´ÏòAjax, HTTPÁ÷£¬WebSocketsÓëSSEÊDz»Í¬µÄ¼¼Êõ¡£¿ÉÄÜ×î´óµÄÓÅÊÆÊǵÍÑÓ³Ù¡£SSEÓÃÓÚwebÓ¦ÓóÌÐòË¢ÐÂÊý¾Ý£¬²»ÐèÒªÓû§×öÈκζ¯×÷¡£
Äã¿ÉÄÜÌý˵¹ýHTML5µÄWebSockets£¬Ò²ÄÜÍÆËÍÊý¾Ýµ½¿Í»§¶Ë¡£WebSocketsÊÇʵÏÖ·þÎñ¶Ë¸ü¼Ó¸´Ôӵļ¼Êõ£¬µ«ËüÊÇÕæµÄÈ«Ë«¹¤socket, ·þÎñ¶ËÄÜÍÆËÍÊý¾Ýµ½¿Í»§¶Ë£¬¿Í»§¶ËÒ²ÄÜÍÆËÍÊý¾Ý»Ø·þÎñ¶Ë¡£SSE¹¤×÷ÓÚ´æÔÚHTTP/HTTPSЭÒ飬֧³Ö´úÀí·þÎñÆ÷ÓëÈÏÖ¤¼¼Êõ¡£SSEÊÇÎı¾Ð­ÒéÄãÄÜÇáÒ׵ĵ÷ÊÔËü¡£Èç¹ûÄãÐèÒª·¢Ëʹ󲿶þ½øÖÆÊý¾Ý´Ó·þÎñ¶Ëµ½¿Í»§¶Ë£¬WebSocketÊǸüºÃµÄÑ¡Ôñ¡£¹ØÓÚSSEÓëWebSocketµÄÇø±ð£¬±¾ÎÄÏÂÃæ»á½²µ½¡£

 

HTML5 ·þÎñÆ÷·¢ËÍʼþ£¨server-sent event£©ÔÊÐíÍøÒ³»ñµÃÀ´×Ô·þÎñÆ÷µÄ¸üÐÂ
Server-Sent ʼþ - µ¥ÏòÏûÏ¢´«µÝ
Server-Sent ʼþÖ¸µÄÊÇÍøÒ³×Ô¶¯»ñÈ¡À´×Ô·þÎñÆ÷µÄ¸üС£
ÒÔÇ°Ò²¿ÉÄÜ×öµ½ÕâÒ»µã£¬Ç°ÌáÊÇÍøÒ³²»µÃ²»Ñ¯ÎÊÊÇ·ñÓпÉÓõĸüС£Í¨¹ý·þÎñÆ÷·¢ËÍʼþ£¬¸üÐÂÄܹ»×Ô¶¯µ½´ï¡£
Àý×Ó£ºFacebook/Twitter ¸üС¢¹À¼Û¸üС¢ÐµIJ©ÎÄ¡¢Èüʽá¹ûµÈ¡£

ä¯ÀÀÆ÷Ö§³Ö£¨ËùÓÐÖ÷Á÷ä¯ÀÀÆ÷¾ùÖ§³Ö·þÎñÆ÷·¢ËÍʼþ£¬³ýÁË Internet Explorer¡££©

EventSource ÍÆËÍ£¨ajaxÆÕͨÂÖѯ£©:

´¦Àí¹ý³Ì£º

¿Í»§¶Ë½¨Á¢EventSource¶ÔÏ󣬶ԷþÎñÆ÷ͨ¹ýhttpЭÒé²»¶Ï½øÐÐÇëÇó¡£·þÎñÆ÷¶Ô¿Í»§¶ËµÄÏìÓ¦Êý¾Ý¸ñʽÓÐËIJ¿·Ö¹¹³É£¬event£¬data£¬id£¬¿Õ¸ñÐС£¿Í»§¶Ë½ÓÊÕµ½·þÎñÆ÷¶ËµÄÏìÓ¦Êý¾ÝÖ®ºó£¬¸ù¾ÝeventʼþÖµ£¬ÕÒµ½EventSource¶ÔÏó¶ÔÓ¦µÄʼþ¼àÌýÆ÷¡£

 

½ÓÊÕ Server-Sent ʼþ֪ͨ
EventSource ¶ÔÏóÓÃÓÚ½ÓÊÕ·þÎñÆ÷·¢ËÍʼþ֪ͨ£º

source = new EventSource("../api/MyAPI/ServerSentEvents"); //ĬÈÏÖ§³Ömessageʼþ source.onmessage = function (event) { console.log(source.readyState); console.log(event); };

ʵÀý½âÎö£º
¡¡¡¡´´½¨Ò»¸öÐ嵀 EventSource ¶ÔÏó£¬È»ºó¹æ¶¨·¢Ë͸üеÄÒ³ÃæµÄ URL£¨±¾ÀýÖÐÊÇ "demo_sse.php"£©£¬²ÎÊýurl¾ÍÊÇ·þÎñÆ÷ÍøÖ·£¬±ØÐëÓ뵱ǰÍøÒ³µÄÍøÖ·ÔÚͬһ¸öÍøÓò£¨domain£©£¬¶øÇÒЭÒéºÍ¶Ë¿Ú¶¼±ØÐëÏàͬ¡£
¡¡¡¡Ã¿½ÓÊÕµ½Ò»´Î¸üУ¬¾Í»á·¢Éú onmessage ʼþ

 

¼ì²â Server-Sent ʼþÖ§³Ö
ÒÔÏÂʵÀý£¬ÎÒÃDZàдÁËÒ»¶Î¶îÍâµÄ´úÂëÀ´¼ì²â·þÎñÆ÷·¢ËÍʼþµÄä¯ÀÀÆ÷Ö§³ÖÇé¿ö£º

if(!!EventSource && typeof(EventSource)!=="undefined") { } else { // ä¯ÀÀÆ÷²»Ö§³Ö Server-Sent.. }

·þÎñÆ÷¶Ë´úÂëʵÀý
ΪÁËÈÃÉÏÃæµÄÀý×Ó¿ÉÒÔÔËÐУ¬Äú»¹ÐèÒªÄܹ»·¢ËÍÊý¾Ý¸üеķþÎñÆ÷£¨±ÈÈç PHP¡¢ASP¡¢ASP.NET¡¢Java£©¡£
·þÎñÆ÷¶ËʼþÁ÷µÄÓï·¨ÊǷdz£¼òµ¥µÄ¡£ÄãÐèÒª°Ñ "Content-Type" ±¨Í·ÉèÖÃΪ "text/event-stream"¡£ÏÖÔÚ£¬Äú¿ÉÒÔ¿ªÊ¼·¢ËÍʼþÁ÷ÁË¡£
ÎÒÖ»»áC#£¬ËùÒÔÓà ASP.NETµÄMVC ÀïÃæµÄApiControllerдÁ˸ö×î¼òµ¥µÄ·þÎñÆ÷¶Ë£º

public class MyAPIController : ApiController { ...api/MyAPI/ServerSentEvents [HttpGet, HttpPost] public Task<HttpResponseMessage> ServerSentEvents() { //Response.ContentType = "text/event-stream" //Response.Expires = -1 //Response.Write("data: " & now()) data = ; HttpResponseMessage response = new HttpResponseMessage { //×¢Ò⣺ContentType = "text/event-stream" Content = ), ) }; return Task.FromResult(response); } }

´úÂë½âÊÍ:
¡¡¡¡°Ñ±¨Í· "Content-Type" ÉèÖÃΪ "text/event-stream"
¡¡¡¡¹æ¶¨²»¶ÔÒ³Ãæ½øÐлº´æ
¡¡¡¡Êä³ö·¢ËÍÈÕÆÚ£¨Ê¼ÖÕÒÔ "data: " ¿ªÍ·£©
¡¡¡¡ÏòÍøҳˢÐÂÊä³öÊý¾Ý

 


EventSource ¶ÔÏó

ÐÂÉú³ÉµÄEventSourceʵÀý¶ÔÏó£¬ÓÐÒ»¸öreadyStateÊôÐÔ£¬±íÃ÷Á¬½ÓËù´¦µÄ״̬¡£

source.readyState
Ëü¿ÉÒÔÈ¡ÒÔÏÂÖµ£º

¡¡¡¡0£¬Ï൱ÓÚ³£Á¿EventSource.CONNECTING£¬±íʾÁ¬½Ó»¹Î´½¨Á¢£¬»òÕßÁ¬½Ó¶ÏÏß¡£

¡¡¡¡1£¬Ï൱ÓÚ³£Á¿EventSource.OPEN£¬±íʾÁ¬½ÓÒѾ­½¨Á¢£¬¿ÉÒÔ½ÓÊÜÊý¾Ý¡£

¡¡¡¡2£¬Ï൱ÓÚ³£Á¿EventSource.CLOSED£¬±íʾÁ¬½ÓÒѶϣ¬ÇÒ²»»áÖØÁ¬¡£

 


ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃÇʹÓà onmessage ʼþÀ´»ñÈ¡ÏûÏ¢¡£²»¹ý»¹¿ÉÒÔʹÓÃÆäËûʼþ£º
ʼþ¡¡¡¡¡¡¡¡ÃèÊö
onopen ¡¡¡¡µ±Í¨Íù·þÎñÆ÷µÄÁ¬½Ó±»´ò¿ª
onmessage µ±½ÓÊÕµ½ÏûÏ¢
onerror ¡¡¡¡µ±·¢Éú´íÎó

 

openʼþ

Á¬½ÓÒ»µ©½¨Á¢£¬¾Í»á´¥·¢openʼþ£¬¿ÉÒÔ¶¨ÒåÏàÓ¦µÄ»Øµ÷º¯Êý¡£

source.onopen = function(event) {
// handle open event
};

// »òÕß

source.addEventListener("open", function(event) {
// handle open event
}, false);
messageʼþ

ÊÕµ½Êý¾Ý¾Í»á´¥·¢messageʼþ¡£

source.onmessage = function(event) {
var data = event.data;
var origin = event.origin;
var lastEventId = event.lastEventId;
// handle message
};

// »òÕß

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • HTML5µÄ±íµ¥ËùÓÐtypeÀàÐÍ - ÆÐÌáÊ÷ϵÄСÎÚ¹ê

    HTML5µÄ±íµ¥ËùÓÐtypeÀàÐÍ - ÆÐÌáÊ÷ϵÄСÎÚ¹ê

    2017-05-16 11:05

  • html5µÄlocalstorageÏê½â - ·çÓêºó¼û²Êºç

    html5µÄlocalstorageÏê½â - ·çÓêºó¼û²Êºç

    2017-05-09 13:00

  • html5µÄweb´æ´¢Ïê½â - Cythia-milk

    html5µÄweb´æ´¢Ïê½â - Cythia-milk

    2017-05-04 16:00

  • HTML5 ½ø½×ϵÁУºÍÏ·Å API ʵÏÖÍÏ·ÅÅÅÐò - _ÁÖöÎ

    HTML5 ½ø½×ϵÁУºÍÏ·Å API ʵÏÖÍÏ·ÅÅÅÐò - _ÁÖöÎ

    2017-05-02 11:02

ÍøÓѵãÆÀ
³