(â»2011/09æ 追è¨: ãã®è¨äºã®æ¹æ³ã¯æ¢ã«å¤ãã®ã§ Socket.IOã¨HTTPã»ãã·ã§ã³ã®å ±æ㯠Socket.IO と Express でセッションの共有 - Block Rockin’ Codes ãªã©ãåèã«ãã¾ããã)
node.jsでchatアプリっぽいもの作るメモ - すぎゃーんメモ
node.js+socket.ioでライブコーディング的なものを作るメモ - すぎゃーんメモ
ã«å¼ãç¶ãã第3å¼¾ã
ç¾å¨ããµã³ãã«ã以ä¸ã®å ´æã§åããã¦ãã¾ãã
http://www1216u.sakura.ne.jp:3001/
ã½ã¼ã¹ã¯ GitHub - sugyan/node-oauth-chat: node.js + socket.io chat (using Twitter OAuth) ã«ç½®ãã¦ããã¾ãã
ã»ãã·ã§ã³å ±æ
Socket.IOã便å©ãªã®ã¯åãã£ãã®ã ãã©ãHTTPã§ã®ãã°ã¤ã³æ
å ±ãªã©sessionæ
å ±ã使ããããã¨ãããã¾ããã
Google グループ ã§ã¯expressã®dynamicHelpersã使ãã¨ã¨ããããã¿ãããªãã¨ãæ¸ãã¦ãããããªãã§ããã試ãã¦ã¿ãã¨ããå°ãªãã¨ãåã®ç°å¢ã§ã¯ãã¾ãããã¾ããã§ããã
ã¨ãããã¨ã§SessionWebSocketã使ãã¾ããSessionWebSocketã«ã¤ãã¦ã¯ä»¥ä¸ã®è¨äºãã¨ã¦ã詳ããã§ãã
Node.js 日本ユーザグループ Blog: HTTP と WebSocket でセッションを共有する
ããã«ãæ¸ãã¦ããéããã©ããæåãããããããã§ãå°ãªãã¨ãç¾å¨ã®0.1.1ã§ã¯secureã¤ãã³ãã®ãã¨messageã渡ãã¾ãããã®ã§pull requestã§æ¾ç½®ããã¦ãã¾ã£ã¦ãã(ï¼)このパッチãå
¥ãã¦ä½¿ãã¾ããã
ä»çµã¿ã¨ãã¦ã¯ãconnnectããåã«Ajaxã§httpãªã¯ã¨ã¹ããè¡ãããµã¼ãã¼å´ã®ããã«ã¦ã§ã¢ã§ãã®http requestããsessionãåå¾ãã¦ç®¡çããããã«ãããã¨ãããã¨ã®ããã§ããã¯ã©ã¤ã¢ã³ãå´ã§ã¯ã
function SessionWebSocket(cb) { $.ajax({ url: "/", dataType: "json", cache : false, beforeSend: function(xhr) { xhr.setRequestHeader("x-access-request-token", "simple"); }, success: function(data) { var socket = new io.Socket(); socket.connect(); socket.send(data["x-access-token"].split(";")[0]); cb(socket); } }); }
ã®ãããªãã®ãç¨æãã¦ããã
SessionWebSocket(function(socket) { socket.on('message', function(msg) { ... }); })
ã¨ããå½¢ã§ã³ã¼ã«ããã¯ã渡ãããã«ãã¦ä½¿ãã¾ãã
ãµã¼ãã¼å´ã¯ãexpressãªã
var express = require('express'); var app = express.createServer(); var io = require('socket.io'); var sws = require('SessionWebSocket')(); app.configure(function() { app.use(express.cookieDecoder()); app.use(express.session()); app.use(sws.http); }); ... var socket = io.listen(app); socket.on('connection', sws.ws( function(client) { client.on('secure', function() { console.log(client.session); }); } ));
ã¨ããããã¡ã§è¨å®ãã¦ä½¿ããã¨ã§ãsocket.ioã®ã³ã¼ã«ããã¯ã«æ¸¡ãããclientããclient.sessionã§httpã®request sessionã«ã¢ã¯ã»ã¹ã§ããããã«ãªãã¿ããã§ãã
ãã°ã¤ã³
ãã°ã¤ã³ã®ä»çµã¿ã¯è²ã
ãªå®è£
æ¹æ³ãèããããã¨æãã¾ãããä»åã¯Twitter OAuthã使ãæ¹æ³ã試ãã¦ã¿ã¾ããã
npmã«"connect-auth"ã¨ããã®ããã£ã¦Twitterã ãã§ãªãGithubãFacebookãè²ã
ãªãã®ã«å¯¾å¿ãã¦ãã°ã¤ã³ããä»çµã¿ãç¨æããã¦ããããã ã£ããã§ããããã¾ãã¡ä½¿ãããªããããåãããªãã£ãã®ã§"oauth"ã ãã¤ãã£ã¦èªåã§å®è£
ãããã¨ã«ãã¾ããã
oauthã§ã®ãã°ã¤ã³ã®å®è£
ã¯ä¸è¨ã®è¨äºãã¨ã¦ãåèã«ãªãã¾ããã
node.js/express で CouchDB をパワーアップ大作戦 - Web屋の人の日記 || WebJourney 開発ログ
... var oauth = new (require('oauth').OAuth)( 'https://api.twitter.com/oauth/request_token', 'https://api.twitter.com/oauth/access_token', '***********************************', // consumer key '***********************************', // consumer secret '1.0', 'http://localhost:3000/signin/twitter', // callback URL 'HMAC-SHA1' ); app.get('/signin/twitter', function(req, res) { var oauth_token = req.query.oauth_token; var oauth_verifier = req.query.oauth_verifier; if (oauth_token && oauth_verifier && req.session.oauth) { oauth.getOAuthAccessToken( oauth_token, null, oauth_verifier, function(error, oauth_access_token, oauth_access_token_secret, results) { if (error) { res.send(error, 500); } else { req.session.user = results.screen_name; res.redirect('/'); } } ); } else { oauth.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) { if (error) { res.send(error, 500); } else { req.session.oauth = { oauth_token: oauth_token, oauth_token_secret: oauth_token_secret, request_token_results: results }; res.redirect('https://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token); } }); } }); ...
æ®éã®ãªã¯ã¨ã¹ããªãtokenãçæãã¦èªè¨¼ãã¼ã¸ã¸ãªãã¤ã¬ã¯ãããã¦ãããcallbackã§è¿ã£ã¦æ¥ãã¨ãã¯oauth_token, oauth_verifierã使ã£ã¦èªè¨¼å¦çãè¡ããæ£ããã¦ã¼ã¶ã¼æ
å ±ãåå¾ã§ããã°sessionã«ã»ãããã¦ä»ã®ãã¼ã¸ã«é£ã°ãã¦ãããã¨ããæµãã§ãã
ã¾ã¨ã
ãã®2ã¤ãçµã¿åããã¦åé ã®ãµã³ãã«ãä½ãã¾ãããæªãã°ã¤ã³ã®ã¦ã¼ã¶ã¼ã¯socket.ioã®sessionIdããTwitterã§ãã°ã¤ã³ãã¦ããã¦ã¼ã¶ã¼ã¯ãã®screen_nameã使ã£ã¦ãã£ããã«æ¸ãè¾¼ããããã«ãã¦ãã¾ãã
ãã£ããã®ä»çµã¿ã¯前々回æ¸ããéããä»åã¯ãã°ãã¡ã¢ãªä¸ã«100件ã¾ã§ä¿æãã¦ããã¦connectæã«ä¸æ°ã«æµãããã«ãã¦ã¿ã¦ãã¾ãã
次ã«è©¦ããããã¨
è¤æ°ã®Socket.IOãã£ãã«ã使ããããªæ¹æ³ãã¾ã ããããã£ã¦ããªãã®ã§ãä¾ãã°ãã©ã¤ãã³ã¼ãã£ã³ã°ããªãã 横ã§ã¯ããã¹ããã£ãããåãã¦ãããã¿ãããªã®ãã©ãå®è£ ããã®ã調ã¹ã¦ããã£ã¦ã¿ããã¨æãã¾ãã