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
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
660
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
130
マスタデータ問題、マイクロサービスでどう解くか
kts
0
140
GoLab2025 Recap
kuro_kurorrr
0
780
「コードは上から下へ読むのが一番」と思った時に、思い出してほしい話
panda728
PRO
39
26k
PostgreSQLで手軽にDuckDBを使う!DuckDB&pg_duckdb入門/osc25hi-duckdb
takahashiikki
0
190
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
480
複雑なUI設計への銀の弾丸 「オブジェクト指向UIデザイン」
teamlab
PRO
2
110
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
140
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
130
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
390
TerraformとStrands AgentsでAmazon Bedrock AgentCoreのSSO認証付きエージェントを量産しよう!
neruneruo
4
1.9k
Featured
See All Featured
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
120
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5k
GraphQLの誤解/rethinking-graphql
sonatard
74
11k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
Docker and Python
trallard
47
3.7k
Heart Work Chapter 1 - Part 1
lfama
PRO
3
35k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
1k
Thoughts on Productivity
jonyablonski
73
5k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
AI: The stuff that nobody shows you
jnunemaker
PRO
1
28
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
31
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