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
280
1
Share
Node.js實戰 即時網頁聊天室
Node.js實戰
即時網頁聊天室
xieren58
November 19, 2012
More Decks by xieren58
See All by xieren58
Python programming text and web mining
xieren58
2
330
Python as numbe rcrunching glue
xieren58
1
93
Other Decks in Programming
See All in Programming
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
190
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
590
PDI: Como Alavancar Sua Carreira e Seu Negócio
marcelgsantos
0
120
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
150
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
120
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
210
ドメインイベントでビジネスロジックを解きほぐす #phpcon_odawara
kajitack
3
740
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.5k
t *testing.T は どこからやってくるの?
otakakot
0
460
ハンズオンで学ぶクラウドネイティブ
tatsukiminami
0
120
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
3
270
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
210
Featured
See All Featured
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
140
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
350
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
730
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
520
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
430
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
220
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.2k
Accessibility Awareness
sabderemane
0
99
Designing for humans not robots
tammielis
254
26k
Balancing Empowerment & Direction
lara
6
1k
Test your architecture with Archunit
thirion
1
2.2k
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