ãã£ãã®50è¡ï¼ node.js ã§ãã¤ã¼ãåãæµããµã¤ãä½ææ³
ä¿®æ£ï¼ããæ°ããã60ãããªãã¦50è¡ã ã£ãããããããã·ã¥ã¿ã°åãæµãã§ãªã好ããªãã¼ã¯ã¼ãï¼ãã¡ããããã·ã¥ããªãã ãã©ï¼ç£è¦ãããã®ã«ãªã£ã¦ãããé©å½ãããã
âã®éãã³ãã³ãæã£ã¦ãã¡ã¤ã«ä½ã£ã¦ããã°ãtwitterã§æå®ãããã¼ã¯ã¼ããç£è¦ãã¦ã ãã ãåãæµããµã¤ããåºæ¥ä¸ããã¾ãã
å¿ è¦ãªãã®
- node.jsã¨npmãã¤ã³ã¹ãã¼ã«ããç°å¢
- é©å½ãªtwitterã¢ã«ã¦ã³ã
ã³ãã³ã
- ã¯ã¼ã¯ã¹ãã¼ã¹ä½ã£ã¦å¿ è¦ã©ã¤ãã©ãªã¤ã³ã¹ã
- linuxãããªãæ¹ã¯ãªãã¨ãªãããã£ã½ããã¨ããã¦ä¸ãã
mkdir twithash cd twithash mkdir views npm install socket.io npm install twitter-node npm install express vi web.js vi views/index.html
web.jsã®ä¸èº«
- ã¢ã«ã¦ã³ãID, ã¢ã«ã¦ã³ããã¹ã¯ã¼ããæ¸ãæãã¦ä¸ãã
- ãã¼ã¯ã¼ãã好ããªãã®ãè¨å®ãã¦ä¸ãããtwitterã®å¶éä¸10åã¾ã§ã¨ãªãã¾ãããhttpsãã¯æ°ãã¡ããã©ããæãã§ãã
const sys = require('sys'), express = require('express'), app = express.createServer(), io = require('socket.io'), TwitterNode = require('twitter-node').TwitterNode; // express app.configure(function() { app.use(express.static(__dirname + '/views')); }); app.listen(8888); // socket.io const socket = io.listen(app); // twitter var twitter = new TwitterNode({ user: 'ã¢ã«ã¦ã³ãID', password: 'ã¢ã«ã¦ã³ããã¹ã¯ã¼ã', track: ['https'] // ããã·ã¥ã¿ã°ã¨ãé©å½ãªåèªã«æ¸ãæããããã }); twitter .addListener('tweet', function(tweet) { socket.broadcast(tweet.text); }) .stream();
index.htmlã®ä¸èº«
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>twithash</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> <script src="/socket.io/socket.io.js"></script> <script> var socket = new io.Socket(); socket.connect(); socket.on('message', function(t) { $('<div></div>') .text(t) .prependTo('#tweets'); }); </script> </head> <body> <div id="tweets"></div> </body> </html>
å®è¡
node web.js
localhost:8888 ã«ã¢ã¯ã»ã¹ããæå®ãããã¼ã¯ã¼ãã®ãã¤ã¼ããã ãã ãåãæµãããã°å®æï¼
ã¶ã£ã¡ãã
ãã³ãã³ãå
¥ãã¦ã50è¡ãªã®ã§ãã¿ãªãããã²ãã£ã¦ã¿ã¦ãã ããï¼ãã¨ãç
½ã£ã¦ããç°å¢æ§ç¯ãä¸çªå¤§å¤ã ã£ã¦ã®ã¯ã¿ããªåãã£ã¦ããããããã
ãã ãç§ã®ç°å¢ã¯ãããvpsã®CentOS 5.5ã§ãå²ã¨ã¤ã¾ããã¯ãªãã£ãã§ãã
twitterã®idã¨pwã¯è¨å®ãã¡ã¤ã«ã¨ãã«å¤åºããã¹ãã§ãããã©ã¤ãã©ãªãããããç¡ãã¦ãé©å½ã«ãã£ã¡ããã°ããããªã¼ã¨æãã¾ãã
å®æããããherokuã®node.jsãã¹ãã£ã³ã°ã§ããã£ã¦ã¿ãã¨è¯ãã§ããã«ã³ã¿ã³ã«å
¨ä¸çã«å
¬éã§ããä¸å®ã®è² è·ãè¶
ããã¾ã§ã¯ç¡æã§ããherokuのドキュメントãè±èªã ãã©åãããããã®ã§ãããã»ã©ã¤ã¾ãããªãã¨æãã¾ãã
ãããnpmã¨herokuã¯ç´ æ´ããããä¸æ¦ç±ãå·ããnode.jsã ã£ããã©ãä¸æ°ã«åçãã¾ããï½
ã³ã¡è¿ã
id:t_43z
ãããªãã§ãããã¼ãç¾ç¶ãherokuã§åããã¦ããµã¤ãã¯chromeã§ãxhr-pollingã«ãªããã§ãè¸å¼µã£ã¦ãhtml5ã ãï¼ãã¨è¨ããªãæãã§ãã
herokuã¯ä½¿ãæå´ã§ãããªãç¶æ
ãªãã§ãããã¡ãã£ã¨ã¡ããã¨èª¿ã¹ããã§ãã
id:mitukiii
expressã¨ãã¢ãsinatraãææ³çã«ããããããªãã§ããã©ããrubyã使ããããã«ãªã£ã¨ããããªã¼ã