Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Node.js實戰 即時網頁聊天室
Search
xieren58
November 19, 2012
Programming
290
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Node.js實戰 即時網頁聊天室
Node.js實戰
即時網頁聊天室
xieren58
November 19, 2012
More Decks by xieren58
See All by xieren58
Python programming text and web mining
xieren58
2
340
Python as numbe rcrunching glue
xieren58
1
100
Other Decks in Programming
See All in Programming
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
5
3.9k
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
180
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
430
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
700
ChromeでローカルLLM
ryu1013
0
140
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
120
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
130
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
400
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
610
ジョジョの世界をXRで再現する
mofurune
0
170
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
30 Presentation Tips
portentint
PRO
1
430
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Navigating Weather and Climate Data
rabernat
0
560
Building Flexible Design Systems
yeseniaperezcruz
330
41k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
New Earth Scene 8
popppiees
4
2.7k
Navigating Team Friction
lara
192
16k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
490
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