跳到主要內容

Server-Sent Events - 造成Server持續push的簡單流程

What is SSEs?

Server-sent events (SSE) is a technology for where a browser gets automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is standardized as part of HTML5[1] by the W3C.

SSEs vs WebSockets

  • SSEs can only push data to the browser.
  • Websockets can both send data to the browsr and receive data from the browser.
  • SSEs is easier to implement.
  • SSEs are send over traditional HTTP.That means they don't require a special protocol to get working.
  • Websockets require full-duplex connections and new Web Socket serves to handle the protocol.

Who does not support it ?

IE & Andriod browser 4.3- doesn't support Server-sen DOM events
➽See more here

How to get working ?

JAVASCRIPT

$(function() {
    if (!window.EventSource) {
        //XHR
        return;
    }
   source.addEventListener('message', function(e) {
       console.log(e.data);
       $rs = $.parseJSON(e.data);
    }, false);

    source.addEventListener('open', function(e) {
      // Connection was opened.
    }, false);

    source.addEventListener('error', function(e) {
       if (e.readyState == EventSource.CLOSED) {
       // Connection was closed.
       }
    }, false);

});

PHP

➽see more

留言

這個網誌中的熱門文章

Indexed Database API - Initail

As a front web developer, I could create UI faster or Optimize file to load page faster. But it hard to faster response time form server however 80-90% of the end-user response time is spent on the frontend. Nowadays we could use the Indexed Database API, or IndexedDB to reduce the request times. Less request times, less response time. What is IndexedDB ? IndexedDB is web browser standard interface for a local database of records holding simple values and hierarchical objects. IndexedDB was initially proposed by Oracle in 2009./* wiki */ What browsers would support IndexedDB ? Most browsers of hybrid devices support it exclude IE9-, Safari 7-,iOS Safari 7.1-, Opera Mini, Android Browser 4.3-./* caniuse.com */ When shall we use IndexedDB ? Save Money on web hosting Data index sort Data keyword search Keep data for period of time How to start it? //*************************************************************** //initDB //{Object}, {data: dbName, objectStore: storeName, ...

最近設計的一組icon..太有APPLE風格被打槍了

jQuery 的slideToggle太好用了

$ ( ".collapse" ) .each ( function ( index, value ) { $ ( this ) . click ( function ( evt ) { evt.preventDefault ( ) ; //prevent the default click event of the anchor tag. var target = $ ( this ) .attr ( "data-toggle" ) ; $ ( "." + target ) .slideToggle ( ) ; } ) ; } ) ; http://code-tricks.com/create-a-common-toggler-for-div-using-jquery/