Shindigã®wave featureã§å®ç¾ããç°¡åãªãçµµãããã£ãã feat. node.js and Socket.IO
ã¿ã¤ãã«æ¸ãã¦ããæ°ã¥ãããã©ãå¥ã«ãã£ããæ©è½ã¨ããªãã£ãã
http://d.hatena.ne.jp/zentoo/20100821/1282407303
以åã«æ¸ããâã®ã¨ã³ããªã§ã¯ãShindigã®extrasã«å ¥ã£ã¦ãwave featureã調ã¹ã¦ã¿ã¦ãããã¼ããShared Stateã¯ã³ã³ããå´ã§ä½ã¨ããã¦ããã£ã¦å½¢ã§ãããã¨ããæã§çµãã£ã¦ãããä»åã¯ããããç¡çãããªããShared Stateã®å®è£ ãè¡ã£ã¦ã¿ãã
Shared Stateã«ç¨ãããµã¼ãã¯node.jsã§å®è£ ãããæ§æã¨ãã¦ã¯ãShindigã8080çªã§å¾ ã¡åããnode with Socket.IOã9000çªã§èµ·åãSocket.IOã¯node.jsã§ç°¡ä¾¿ã«WebSocketã®ãµã¼ããµã¤ããå®ç¾ããã©ã¤ãã©ãªã¨ãã¦ç¥ããã¦ããããWebSocketãåããªãç°å¢ã§ã¯xhr-multipartãxhr-pollingã«fallbackããããã«ãªã£ã¦ããããããã£ããµã¼ãå´ã©ã¤ãã©ãªã®ä¾ã«æ¼ãããã¯ã©ã¤ã¢ã³ãå´ã«å¿ è¦ãªã©ã¤ãã©ãªããµã¼ãããããã®å£ãç¨æãã¦ãããSocket.IOã§ã¯
http://{host}/socket.io/socket.io.js
ãããã«ç¸å½ãããã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã¯ä¸è¨ã«ä¸ãã以å¤ã«ãæ§ã ãªãããã³ã«ã«å¯¾å¿ãã¦ãããnode.jsã¨ã¯ç¬ç«ã«ä½¿ããã¨ãã§ããããã ã
http://github.com/LearnBoost/Socket.IO
Shindigã®wave featureã§ã¯stateã®æ´æ°ãèµ·ãã£ãå ´åã«gadgets.rpcã使ã£ã¦ã¾ãcontainerå´ã«å¦çã移ããããããä½ããã®å½¢ã§Shared Stateãå®ç¾ããããã®åãå£ã«ã¢ã¯ã»ã¹ãããã¨ããå½¢å¼ã«ãªã£ã¦ããããªã®ã§ãå¤å°ã¾ã©ãã£ããããä»åã®wave gadgetã«ãããéä¿¡ã®æµããæ¸ãã¨ä»¥ä¸ã®ããã«ãªãã
wave gadgetã§stateãæ´æ° â gadgets.rpcã§æ´æ°ãã¼ã¿ãcontainerå´ã¸ â container side JSãããShared Stateãµã¼ãï¼ä»®ç§°ï¼ã¸ãã¼ã¿ãæãã â Shared Stateãµã¼ãããã¼ã¿ãåãåãããã®ä»ã®ã¯ã©ã¤ã¢ã³ãã¸ããã¼ããã£ã¹ã â containerå´JSãåãåã â gadgets.rpcã§wave gadget㸠â wave.setStateCallback()ã§æå®ããã³ã¼ã«ããã¯é¢æ°ãå¼ã°ãã
ãã¼ã¾ã©ãã£ããããã¨ã«ãããããªãããShared Stateãµã¼ããã¯ã©ã¤ã¢ã³ãããéä¿¡ããããã¼ã¿ãåãåããä»ã®ã¯ã©ã¤ã¢ã³ãã«ããã¼ããã£ã¹ãï¼ãã¡ããpushï¼ãããã¨ããé¨åããµã¼ããµã¤ãã®ãã¢ã«ãªãããã ããä»åã¯Socket.IOããã®è¾ºããããããã£ã¦ãããã®ã§ã該å½é¨åã®ã³ã¼ãã¯ä»¥ä¸ã®ããã«ãªãã
socket.on('connection', function(client) { client.on("message", function(data) { client.broadcast(data); }); });
çã£ï¼ã¨ããæãã ããSocket.IOã«ã¯broadcast()ã¨ããååã§ç¾å¨ãµã¼ãã«æ¥ç¶ãã¦ããä»ã®ã¯ã©ã¤ã¢ã³ãã«ã¡ãã»ã¼ã¸ãããã¼ããã£ã¹ããããã®ãã®ãºããªãªã¡ã½ãããããã®ã§ãã»ã¨ãã©ãããã¨ã¯ãªãã
ãã¡ãããããªé¢¨ã«ããã¨å ¨ã¦ã®wave APIã使ã£ã¦ããã¬ã¸ã§ããã®éä¿¡ãã¼ã¿ãå ¨é¨æ··ãã£ã¦ãã¾ãã®ã§ãç¾å®ã«ã¯ãããªå®è£ ããããã¨ã¯ãªãã ãããèªè¨¼ãæ°¸ç¶åã®ä»çµã¿ãå¿ è¦ã«ãªããã§ãä»åã¯ãã¨ã«ããShindigã®wave featureãåããããã¨ãç®çãªã®ã§ãã®è¾ºã¯å²æã
ããã«ãã£ã¦Socket.IOçµç±ã§ã¯ã©ã¤ã¢ã³ãéã®ã¡ãã»ã¼ã¸ã³ã°ãè¡ããã¨ãå¯è½ã«ãªã£ãã®ã§ãå¾ã¯containerå´ã®htmlã§ã¯ã©ã¤ã¢ã³ãå´ã©ã¤ãã©ãªãèªã¿è¾¼ãã§ãè³ç«ã¦ããã¦ããã ãã
<script type="text/javascript" src="http://localhost:9000/socket.io/socket.io.js"></script> <script type="text/javascript"> var socket = new io.Socket("localhost", { port: 9000 }); function initRpcs() { gadgets.rpc.register("wave_gadget_state", function(data) { socket.send( { type: "gadget_state", content: data } ); }); gadgets.rpc.register("wave_private_gadget_state", function(data) { socket.send( { type: "private_gadget_state", content: data } ); }); } function initSocket(version) { var frameId = "remote_iframe_0"; socket.on('message', function(data) { gadgets.rpc.call(frameId, "wave_" + data.type, null, data.content); }); socket.connect(); } gadgets.rpc.register('wave_enable', initSocket); initRpcs(); </script>
ã¡ãªã¿ã«ä¸è¨ã³ã¼ãã®frameIdã¨ãã¯ãShindigä»å±ã®samplecontainerã§åããåæã§æ±ºãæã¡ã§ãã
ã§ãwave APIã®ãã¡stateé¢ä¿ã®APIãåãããã«ãªã£ããã ãã©ããããã使ã£ã¦ä½ããããã¨ãããã¨ã«ãªã£ãã®ã§ãã¨ããããcanvasã使ã£ããçµµããå ±æã¢ããªï¼ãã£ã¡ãåç´ãªãã¤ï¼ãä½ã£ã¦ã¿ãã以ä¸ãããã¼ã«ã«ã§ãã®ãçµµããã¢ã㪠on wave APIãåããã¦ããåç»ã
å ã®ç»é¢ããã«ãã®ã§ã¡ãã£ã¨è§£ãã¥ããããå·¦ã§Firefoxãå³ã§Chromeãç«ã¡ä¸ãã¦ãããä¸æ¹ã®ãã©ã¦ã¶ã§æ¸ããç·ã®åº§æ¨ãã¼ã¿ãwave.getState().submitValue()ã«ãã£ã¦gadgets.rpcçµç±ã§nodeã¸éãããnodeãclient.broadcast()ã«ãã£ã¦ä»æ¹ã®ãã©ã¦ã¶ã¸éä¿¡ãããã¨ã§canvasã®åæãå®ç¾ãã¦ãããã¡ãã£ã¨å¤ã ããèªåã®æããç·ã¯èµ¤è²ã§ãä»ã®ã¯ã©ã¤ã¢ã³ãããæç»ããç·ã¯éè²ã«ãªãã¨ããä»æ§ã
ãã®ä¾ã§ã¯ããæç»ããï¼ã¨ãããfillRectããï¼ç¹ã®åº§æ¨ããããã¡ã«çªã£è¾¼ãã§ããããããã¡ãä¸å®ã¾ã§åã¾ã£ãããã®ãã¼ã¿ãsubmitDeltaãã â åãåã£ãå´ã¯é åããªãã¦æç»ãã¨ãããã¤ã¼ããªã¢ã«ã´ãªãºã ã§åæãè¡ã£ã¦ãããcanvasã®åæã«å®ç³ã¢ã«ã´ãªãºã ï¼ããããã©ããã¯åãããªãããç¾å®ã«ã¯ãã£ã¨ãã·ãªæ¹æ³ã使ãã ãããã§ããã ããµã¼ãå´ã¨ã®éä¿¡ãæãã¤ã¤ãã¹ã ã¼ãºãªåæãè¡ãæ¹æ³â¦ãã¼ããã³ãã ãWebSocketããã©ã®ãã©ã¦ã¶ã§ã使ããããã«ãªãã®ã¯å ã ãããã
ã¨ã«ãããã«ããwave featureãåãããã¨ã«ã¯æåããã®ã§ãã§ãããã§ãããzentoooå çã®Shindigã®ä¸ã§åãwave gadgetãè¦ããã®ã¯æã¨åæ°ã¨ç¼¶ãã¼ã«ã ãï¼
ä¸å¿ã§ã¯ããããã½ã¼ã¹ãããã¨ããã
http://github.com/zentooo/shindig-wave-with-node
ã¯ããã¦ã®Node.js -ãµã¼ãã¼ãµã¤ãJavaScriptã§Webã¢ããªãéçºãã-
- ä½è : æ¾å³¶æµ©é
- åºç社/ã¡ã¼ã«ã¼: ã½ãããã³ã¯ã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2013/03/15
- ã¡ãã£ã¢: 大åæ¬
- ã¯ãªãã¯: 15å
- ãã®ååãå«ãããã° (5件) ãè¦ã