HTML5 API - APPCACHE



HTML5 APPCACHE 방식의 최대 장점으로는 오프라인 작업 작업을 구현할 수 있다는 것입니다.


즉, 데스크탑 및 모바일 기기의 통신이 끊어진 경우에도 어플리케이션 작업이 가능하다는 말이며,


이를 통해 모바일과 같은 환경(네트웍 속도가 느린..)의 사용자는 불필요한 HTTP 요청(이미 캐쉬 되어 있으므로 304(조건부 재요청)를 일으키지 않는다.)에 대한 네트웍 비용을 줄임으로써 좀 더 빠른 응답을 받아볼 수 있을 것입니다.





더 자세한 설명은 API 작성 시 참조한 링크를 대신합니다.




1. APPCACHE API 코드:


  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="m.aspx.cs" Inherits="FPSCamp.Potal.mobile.m" %>
  2. <!DOCTYPE HTML>
  3. <html manifest="cache.manifest">
  4. <head>
  5.     <title>Manifest</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //<![CDATA[
  10.    
  11.     // onLine, offLine
  12.     var Connection = (function (win, doc) {
  13.  
  14.         return new (function () {
  15.  
  16.             function init() {
  17.                 return this;
  18.             };
  19.  
  20.             init.prototype = {
  21.                 isOnline: function () {
  22.                     return win.navigator.onLine;
  23.                 }
  24.             };
  25.  
  26.             return init;
  27.         } ());
  28.  
  29.     })(window, document);
  30.  
  31.     // appCache
  32.     var Cache = (function (win, doc) {
  33.  
  34.         return function (opt) {
  35.  
  36.             function init() {
  37.  
  38.                 Connection.isOnline() && win.applicationCache && (function () {
  39.  
  40.                     this.cache = win.applicationCache;
  41.  
  42.                     // events
  43.                     this.onchecking = function () { ; };
  44.                     this.onnoupdate = function () { ; };
  45.                     this.onupdateready = function () { ; };
  46.                     this.onobsolete = function () { ; };
  47.                     this.ondownloading = function () { ; };
  48.                     this.oncached = function () { ; };
  49.                     this.onerror = function () { ; };
  50.  
  51.                     //add evnets
  52.                     appendEvent.call(this);
  53.  
  54.                     extend.call(this, opt);
  55.  
  56.                 }).call(this);
  57.  
  58.                 return this;
  59.             };
  60.  
  61.             init.prototype =
  62.             {
  63.                 update: update,
  64.                 getStatus: getStatus
  65.             };
  66.  
  67.             return new init();
  68.  
  69.         };
  70.  
  71.  
  72.         function appendEvent() {
  73.  
  74.             var that = this;
  75.  
  76.             bind(this.cache, 'checking', function (e) { that.onchecking.apply(that, [e]); });
  77.             bind(this.cache, 'noupdate', function (e) { that.onnoupdate.apply(that, [e]); });
  78.             bind(this.cache, 'updateready', function (e) { that.onupdateready.apply(that, [e]); });
  79.             bind(this.cache, 'obsolete', function (e) { that.onobsolete.apply(that, [e]); });
  80.             bind(this.cache, 'downloading', function (e) { that.ondownloading.apply(that, [e]); });
  81.             bind(this.cache, 'cached', function (e) { that.oncached.apply(that, [e]); });
  82.  
  83.             // exception
  84.             bind(this.cache, 'error', function (e) { that.onerror.apply(that, [e]); });
  85.         };
  86.  
  87.         function update() {
  88.             try {
  89.                 Connection.isOnline() && this.cache.update();
  90.             }
  91.             catch (e) {
  92.                 this.cache.onerror();
  93.             }
  94.         };
  95.  
  96.         function getStatus() {
  97.             return getStatusText(this.cache.status);
  98.         };
  99.  
  100.         function getStatusText(status) {
  101.             return ['uncached', 'idle', 'checking', 'downloading', 'updateready', 'obsolete'][status];
  102.         };
  103.  
  104.  
  105.         // 객체 상속 함수
  106.         function extend() {
  107.  
  108.             var target = this
  109.           , opts = []
  110.           , src = null
  111.           , copy = null;
  112.  
  113.             for (var i = 0, length = arguments.length; i < length; i++) {
  114.  
  115.                 opts = arguments[i];
  116.  
  117.                 for (var n in opts) {
  118.                     n = n.toLowerCase();
  119.                     src = target[n];
  120.                     copy = opts[n];
  121.  
  122.                     if (src === copy) continue;
  123.                     if (copy) target[n] = copy;
  124.                 }
  125.             }
  126.         };
  127.  
  128.         return init;
  129.  
  130.     })(window, document);
  131.  
  132.     var app = Cache({
  133.         onchecking: function () {
  134.             alert('checking');
  135.         },
  136.         onnoupdate: function () {
  137.             alert('noupdate');
  138.         },
  139.         onupdateready: function () {
  140.             alert('cheupdatereadycking');
  141.         },
  142.         onobsolete: function () {
  143.             alert('obsolete');
  144.         },
  145.         ondownloading: function () {
  146.             alert('downloading');
  147.         },
  148.         oncached: function () {
  149.             alert('cached');
  150.         }
  151.     });
  152.  
  153.     // 이벤트 할당
  154.     function bind(elem, type, handler, capture) {
  155.         type = typeof type === 'string' && type || '';
  156.         handler = handler || function () { ; };
  157.  
  158.         if (elem.addEventListener) {
  159.             elem.addEventListener(type, handler, capture);
  160.         }
  161.         else if (elem.attachEvent) {
  162.             elem.attachEvent('on' + type, handler);
  163.         }
  164.  
  165.         return elem;
  166.     };
  167.  
  168.  
  169. //]]>
  170. </script>
  171. <input type="button" value="appUpdate" onclick="app.update()" />
  172. <img src="1.jpg" alt="1" title="1" />
  173. </body>
  174. </html>





2. manifest 파일을 위한 IIS Content-Type 등록:


 

위와 같이 해당 사이트의 등록정보를 수정합니다.





3. Cache 등록 화면:



위와 같이 해당 menifest 파일이 등록되었으며, 그에 따른 이벤트들이 각가 실행되고 있다.





결과:


브라우저 지원차이 등의 여러 가지 테스트를 대부분의 표준 브라우저에서 시도했지만, 각 브라우저에 대한 결과만 조금씩 달랐을 뿐, 네트웍이 분리된 상태의 작업(오프라인 작업)은 가능하지 않았습니다. 정확히 말해 manifest에 등록된 Offline 리소스들을 가져오지 못하는 문제였습니다.;;;



이 기능에 대한 테스트는 여기서 일단락 지을 것이며, 만약 정확한 테스트를 마치신 분이 계신다면, 댓글 남겨주시면 감사하겠습니다.  꾸벅~~ ^^;;





참고 사이트:


@clearboth:

http://html5.clearboth.org/offline.html


@박종명님:

http://m.mkexdev.net/60