Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Node.js實戰 即時網頁聊天室
Search
xieren58
November 19, 2012
Programming
1
280
Node.js實戰 即時網頁聊天室
Node.js實戰
即時網頁聊天室
xieren58
November 19, 2012
Tweet
Share
More Decks by xieren58
See All by xieren58
Python programming text and web mining
xieren58
2
330
Python as numbe rcrunching glue
xieren58
1
90
Other Decks in Programming
See All in Programming
Basic Architectures
denyspoltorak
0
130
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
480
Developing static sites with Ruby
okuramasafumi
0
330
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
160
Navigating Dependency Injection with Metro
l2hyunwoo
1
190
PC-6001でPSG曲を鳴らすまでを全部NetBSD上の Makefile に押し込んでみた / osc2025hiroshima
tsutsui
0
190
perlをWebAssembly上で動かすと何が嬉しいの??? / Where does Perl-on-Wasm actually make sense?
mackee
0
200
Cell-Based Architecture
larchanjo
0
150
複雑なUI設計への銀の弾丸 「オブジェクト指向UIデザイン」
teamlab
PRO
2
110
ローカルLLMを⽤いてコード補完を⾏う VSCode拡張機能を作ってみた
nearme_tech
PRO
0
180
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
6
4k
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
490
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Marketing to machines
jonoalderson
1
4.4k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
71
End of SEO as We Know It (SMX Advanced Version)
ipullrank
2
3.8k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
48
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
The World Runs on Bad Software
bkeepers
PRO
72
12k
ラッコキーワード サービス紹介資料
rakko
0
1.8M
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
0
100
Transcript
Node.js 實戰 即時網頁聊天室
聊天室 網頁介面 即時
聊天室 網頁介面 即時 架網站 全雙工
聊天室 Express Socket.io 架網站 全雙工
Express http://expressjs.com
小範例 var express = require('express'); var app = express.createServer(); app.get('/',
function(req, res){ res.send('Hello World'); }); app.listen(3000);
安裝 + 懶人專案生成 npm install -g express express chatroom
閹割 public/ routes/ views/ app.js Package.json
app.js var express = require('express'); , routes = require('./routes'); app.configure(function(){
app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); app.get('/', routes.index);
public/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Chatroom</title> <link href="stylesheets/style.css"
rel="stylesheet" type="text/css"> <script src="javascripts/jquery-1.7.2.min.js"></script> </head> <body> </body> </html>
Socket.io http://socket.io
小範例 瀏覽器端 socket.on('hello', function (data) { console.log(data); socket.emit('hey', 'hey server');
}); 伺服器端 socket.emit('hello', 'hello client'); socket.on('hey', function (data) { console.log(data); });
安裝 npm install socket.io
app.js var app = module.exports = express.createServer(), io = require('socket.io').listen(app);
public/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Chatroom</title> <link href="stylesheets/style.css"
rel="stylesheet" type="text/css"> <script src="javascripts/jquery-1.7.2.min.js"></script> <script src="/socket.io/socket.io.js"></script> </head> <body> </body> </html>
None
app.js // 當有客戶端連線進來 ... io.sockets.on('connection', function (socket) { socket.emit('hello', 'hello
client'); socket.on('hey', function (data) { console.log(data); }); });
public/javascripts/main.js // 連線到伺服器 var socket = io.connect(); socket.on('hello', function (data)
{ console.log(data); socket.emit('hey', 'hey server'); })
結果 瀏覽器端 hello client 伺服器端 hey server
恭喜你剛 成功試驗 Node–Express–Socket.io 殺手組合
剩下的靠你自己 :p