ã¯ããã«
ããç°¡åãªHTTPãµã¼ãã®èªä½ã第5åç®ã®ä»åã¯ãã¯ã©ã¤ã¢ã³ãã¨ã®éä¿¡ã«ããã©ã¦ã¶èªèº«ã«ãããªã¯ã¨ã¹ãï¼ã¬ã¹ãã³ã¹ã«å ãã¦Ajaxã¨Cometãå©ç¨ããç°¡åãªãã£ããã¢ããªã±ã¼ã·ã§ã³ãå®è£ ãã¾ãã
ãWebã¢ããªã±ã¼ã·ã§ã³ã§ãã£ãããå®è£ ããå ´åã«èããªããã°ãªããªãã®ã¯ãä»ã®çºè¨è ã®ã¡ãã»ã¼ã¸ãã©ããã£ã¦ãã©ã¦ã¶ã¸åæ ããããã§ããèªåãçºè¨ããå ´åã¯ããµã¼ãã¸å¯¾ãã¦ã¡ãã»ã¼ã¸ãéä¿¡ããã¾ããå¾ã£ã¦å¿çãã¼ã¿ã«ç¾æç¹ã®çºè¨ãè¿ãã°æ¸ã¿ã¾ãããããããã®æ¹æ³ã ãã§ã¯ä»ã®åå è ã®çºè¨ãçºè¨æã«èªããã¨ã¯ã§ãã¾ããã
ããã®åé¡ã解決ããæ段ã¯è¤æ°ããã¾ãã
ã次ã®å³ã¯æãåå§çã§ããã確å®ãªãF5é£ææ³ããå³ç¤ºãããã®ã§ãã
ãF5é£ææ³ã§ã¯ãã¦ã¼ã¶ã¼ã¯ææ°ã®çºè¨ãããããã ã¨æããæã«ï¼»F5ï¼½ãå©ãã¾ããããã¨ãã©ã¦ã¶ãææ°ã®æ å ±ããµã¼ãã¸åãã«è¡ããããããçºè¨ãæ´æ°ããã¦ããã°ãã®æ å ±ã表示ã§ãã¾ãã
ããããããã®æ¹æ³ã¯çºè¨ãæ´æ°ããã¦ããªãå ´åã«ãç¡é§ãªãªã¯ã¨ã¹ããè¡ããããããµã¼ãã«ã¨ã£ã¦è² è·ãé«ããçµæçã«ã¦ã¼ã¶ã¼ã«ã¨ã£ã¦ã¯ãªããªãæ´æ°ãããªãã¨ãã£ãå¼å®³ãæã¡ã¾ãã
ã次ã®å³ã¯ãF5é£ææ³ãæ¹åãããã©ã¦ã¶èªèº«ã®æ©è½ãå©ç¨ãã¦åèªã¿è¾¼ã¿ãè¡ããMETA Refreshæ³ããå³ç¤ºãããã®ã§ãã
ãMETA Refreshæ³ã¯HTMLã®META
ã¿ã°ãå©ç¨ãã¦ä¸å®æéãã¨ã«ããã©ã¦ã¶ã«ãµã¼ãã®ææ°ã®æ
å ±ãèªã¾ãã¾ãããã®æ¹æ³ã¯ãèªååããã¦ããã¨ããé·æã¯ãããã®ã®ãF5é£ææ³ã®çæã«å ãã¦ãæ´æ°ééãçãããã¨å
¥åããã«ãããªãï¼å ´åã«ãã£ã¦ã¯å
¥åã§ããªãï¼ãã¨ããæ´æ°ééãé·ãããã¨åæ ãé
ããã¨ãã£ãçæãå ããã¾ãã
ãAjaxãå©ç¨ããã¨ãMETA Refreshæ³ã®çæã§ããå ¥åãã«ããã¨ããåé¡ã¯èµ·ãã¾ããã
ãããããAjaxãå©ç¨ãã¦ããµã¼ãã¸ã®ç¡é§ãªèªã¿åãè¦æ±ã¯è§£æ¶ãã¾ãããã¾ããæ´æ°ééãé·ãã¨ãã¨åæ ãé ããã¨ããåé¡ç¹ã¯æ®ã£ãã¾ã¾ã§ãã
ãCometã¯ãAjaxãå©ç¨ããã®ã¨åãXmlHttpRequest
ï¼XHRã¨ç¥ããããã¨ãããã¾ãï¼ãªãã¸ã§ã¯ããå©ç¨ãã¾ãããWebãµã¼ãå´ã§ãã¼ã¿ãæ´æ°ãããã¾ã§å¿çãè¿ããªãã¨ããéããããã¾ãããã®ãããç¡é§ãªèªã¿è¾¼ã¿ãæããããã¨åæã«ãå³åº§ã«æ´æ°ãåæ ã§ãã¾ãã
ãæ¬è¨äºã§ã¯ãååä½æããéãããã¯IOãå©ç¨ããWebãµã¼ãã®å®è£ ã«Cometç¨ã®å®è£ ã¨ãã£ããã¢ããªã±ã¼ã·ã§ã³ã追å ãã¦ãå®éã®Cometã®åä½ã確èªã§ããããã«ãã¾ãã
ããªããCometã®å®è£ æ¹æ³ã«ã¯ãã¯ã©ã¤ã¢ã³ãå´ããã¼ã¿åå¾ã®ã¤ã©åæ¥ç¶ããããã³ã°ãã¼ãªã³ã°ãã¨å¼ã°ããæ¹æ³ã¨ãåä¸ã³ãã¯ã·ã§ã³ä¸ã§ãã¼ã¿ãåå²ãã¦éãããã«ããã¼ãXmlHttpRequestãã¨å¼ã°ããæ¹æ³ãããã¾ãããããã§ã¯è¤æ°ã®ãã©ã¦ã¶ã§åä½å¯è½ãªãã³ã°ãã¼ãªã³ã°ãå©ç¨ãã¾ãã
対象èªè
ãæ¬è¨äºã¯ãJavaããã°ã©ãã³ã°ã®ä¸ç´è 以ä¸ã対象ã«ãCometã¨ããæ¯è¼çæ°ããWebæè¡ã解説ãããã¨ãç®çã¨ãã¾ããJavaã®ããã°ã©ã èªä½ã¯ååã®è¨äºãNIOã®éãããã¯æ¥ç¶ãå©ç¨ããå¤éIOã®å®è£ ãã§ç¤ºãããã®ã«å¤å°æãå ããã ããªã®ã§ã解説ã¯çç¥ãã¾ãã
ããã ãCometãå®è£ ããã«ã¯ãµã¼ãå´ã ãã§ã¯ãªããã¯ã©ã¤ã¢ã³ãå´ã«ã¤ãã¦ãAjaxãå©ç¨ããããã®JavaScriptããã°ã©ãã³ã°ãå¿ è¦ã§ãããããéä¿¡ã«å©ç¨ããXmlHttpRequestã¯ãã©ã¦ã¶ã«ãã£ã¦å©ç¨æ¹æ³ãç°ãªãã¾ããã¾ããã¹ã¦ã®å¦çãJavaScriptã§è¨è¿°ããã¨ã³ã¼ããç ©éã«ãªã£ã¦ãã¾ãã¾ãã
ããã®ãããããã§ã¯ã¯ã©ã¤ã¢ã³ãå´ã®è¨è¿°ã«ãprototype.jsãã¨ããAjaxç¨JavaScriptã©ã¤ãã©ãªï¼MITã©ã¤ã»ã³ã¹ï¼ãå©ç¨ãã¾ãã
ãå¾ã£ã¦ãæ¬è¨äºã§ã¯ãprototype.jsããå©ç¨ããAjaxã®å©ç¨æ¹æ³ã«ã¤ãã¦ã¯ç°¡åã«è§£èª¬ãã¾ãã
å¿ è¦ãªç°å¢
ãæ¬è¨äºã®ã½ã¼ã¹ããã«ãï¼å®è¡ããã«ã¯ãJ2SE 5.0以ä¸ãå©ç¨ãã¦ãã ãããã½ã¼ã¹ãã¡ã¤ã«ã¢ã¼ã«ã¤ãã¯ãç´æ¥NetBeans 5.0ã®ããã¸ã§ã¯ãã¨ãã¦éããããã«æ§æãã¦ããã¾ãããã ããNetBeans 5.0ãå©ç¨ããªãã¦ããç´æ¥ã³ãã³ãã©ã¤ã³ããJDKããã³Antãå©ç¨ãã¦ãã«ããããã¨ãã§ãã¾ããã¾ãããã£ããã¢ããªã±ã¼ã·ã§ã³ãå®è¡ããã«ã¯ããprototype.jsããå¿ è¦ã§ããæ¬è¨äºã®ä½æã«å©ç¨ãããprototype.jsãã®ãã¼ã¸ã§ã³ã¯1.5ã§ãã
ããªããã½ã¼ã¹ãã¡ã¤ã«ã¢ã¼ã«ã¤ãã«ã¯ããããããJ2SE 5.0ã§ãã«ãããã¯ã©ã¹ãã¡ã¤ã«ã¨jarãããã³ãprototype.jsããå«ãã¦ãã¾ãããã®ãããJREãããã°ãã£ããã¢ããªã±ã¼ã·ã§ã³ã試ããã¨ãã§ãã¾ãã
ãåèã¾ã§ã«çè ãå©ç¨ããæ¬è¨äºã®ãã¹ãç°å¢ã¯ä»¥ä¸ã®ãã®ã§ãã
OS | J2SE | Ant | IDE | ãã©ã¦ã¶ |
Windows XP | 1.5.0_09 | 1.6.5 | NetBeans5.0J | Firefox 2.0/IE6SP2 |
OS X | - | - | - | Safari 2.0.4 |
ã³ãã³ãã©ã¤ã³ããã®å®è¡ã«ã¤ãã¦
ãNetBeansãå©ç¨ããã«ã³ãã³ãã©ã¤ã³ããå®è¡ããã«ã¯ãJavaã¨Antãå®è¡ã§ããããã«PATHãè¨å®ããã¢ã¼ã«ã¤ããå±éãããã£ã¬ã¯ããªã®ä¸ï¼ãbuild.xmlããåå¨ãããã£ã¬ã¯ããªï¼ã§
ant run
ããå®è¡ãã¦ãã ãããããã«ãããã¼ã8801ã§ãã£ããã¢ããªã±ã¼ã·ã§ã³ããµã¼ãã¹ãéå§ãã¾ãã
ããã£ããã¢ããªã±ã¼ã·ã§ã³ã®URLã¯ããhttp://localhost:8801/html/chat.htmlãã§ããä»ã®ãã·ã³ããã¢ã¯ã»ã¹ããå ´åã«ã¯ãlocalhostã®ç®æãå®è¡ãã¦ãããã·ã³ã®ãã®ã«å¤ãã¦ãã ãããã¾ãããã¼ã½ãã«ãã¡ã¤ã¢ã¦ã©ã¼ã«ãªã©ã§ãã¼ã8801ãå¡ãã§ããå ´åã«ã¯äºåã«éæ¾ãã¦ããå¿ è¦ãããã¾ãã
ãçµäºããã«ã¯ãã³ãã³ãã©ã¤ã³ã§ãã¬ã¤ã¯ããããï¼Windowsã§ããã°ï¼»Ctrlï¼½+ï¼»Cï¼½ãæ¼ãï¼ã¾ãã¯ããã©ã¦ã¶ãããhttp://localhost:8801/quitãã«ã¢ã¯ã»ã¹ãã¦ãã ããã
注æäºé
ãåãã©ã¦ã¶ãHTTP 1.1ã§ç¨¼åããåä¸ãã¹ãã«å¯¾ãã¦åæã«å¼ã³åºããXmlHttpRequestã®æ°ã«ã¯ã2åã¨ããå¶éãããã¾ãããã®ã2ãã¨ããæ°åã¯RFC2616ã§ç¡æ¡ä»¶æºæ ã®è¦ä»¶ã¨ãã¦è¦å®ããããã®ã§ãããã®ãããä¾ãã°IEãå©ç¨ãã¦ããå ´åã§ããã°ãï¼»ãã¡ã¤ã«ï¼½-ï¼»æ°ããã¦ã£ã³ãã¦ï¼½ãå®è¡ããã¨ã2ã¤ã®ã¦ã£ã³ãã¦ãXmlHttpRequestã®å©ç¨å¾ ã¡ã§ãããããã¯ãã¾ãï¼ä»ã®ãã©ã¦ã¶ããçºè¨ãããã¨ã§ãã¼ãªã³ã°ãå®äºããã¦ä¸æçã«ããã¯ã解é¤ã§ãã¾ãï¼ããããé¿ããã«ã¯ãè¤æ°ã®ããã»ã¹ã§å®è¡ããããç°ãªããã©ã¦ã¶ãå©ç¨ããããå¥ã®ãã·ã³ããå¼ã³åºããããã¦ãã ããã
ãã¾ããæ¬å®è£ ã¯ä¸å¿åä½ãã¾ããå®æããã¢ããªã±ã¼ã·ã§ã³ã§ã¯ããã¾ããããã®ããããªãã¼ããã¦ããã³ã°ãã¼ãªã³ã°ãã¦ããXmlHttpRequestã¯ãã¼ãªã³ã°ãç¶ç¶ããã¾ã¾ã¨ãªãããæ°ããã¦ã£ã³ãã¦ããä½ã£ãã®ã¨åãç¶æ ã¨ãªãã¾ãï¼ãã ããã®ç¶æ ã¯ä¸å®æéçµéå¾ã«å ã®ãªã¯ã¨ã¹ããå»æ£ããã¦è§£æ¶ãã¾ãï¼ããã¼ã¸ã®ãªãã¼ããå¿ è¦ãªå ´åã«ã¯ããªãã¼ãå¾ã«ä»ã®ãã©ã¦ã¶ããçºè¨ãã¦ãã¼ãªã³ã°ãå®äºãããããã¾ãã¯ãã©ã¦ã¶ãåèµ·åãã¦ãã ããã
NetBeans 5.0ããã®å©ç¨
ãã¡ãã¥ã¼ããï¼»ãã¡ã¤ã«ï¼½-ï¼»ããã¸ã§ã¯ããéãï¼½ãé ã«é¸æããã½ã¼ã¹ãã¡ã¤ã«ã¢ã¼ã«ã¤ããå±éãããã£ã¬ã¯ããªã®ãhttpserverããã£ã¬ã¯ããªãé¸æãã¦ãï¼»ããã¸ã§ã¯ããã©ã«ããéãï¼½ãã¯ãªãã¯ãã¦ãã ããã