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
Socket.io 即時通訊實作
Search
Tommy Chen
December 04, 2014
Programming
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Socket.io 即時通訊實作
Tommy Chen
December 04, 2014
More Decks by Tommy Chen
See All by Tommy Chen
Kosko - 改用 JavaScript 來管理 Kubernetes YAML (Kubernetes Summit 2021)
tommy351
1
1.4k
Kosko - 改用 JavaScript 來管理 Kubernetes YAML (COSCUP 2021)
tommy351
0
110
Kubernetes 101
tommy351
2
310
Fast Web Development with Express
tommy351
0
120
An Introduction to Node.js
tommy351
1
340
Other Decks in Programming
See All in Programming
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
180
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
190
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
480
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
230
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
380
Building a Meta Ray-Ban display app
akkeylab
0
150
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
220
Google Apps Script で Ruby を動かす
kawahara
0
220
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
0
1.8k
今さら聞けない .NET CLI
htkym
0
190
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
790
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
690
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
240
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Ruling the World: When Life Gets Gamed
codingconduct
0
300
The Pragmatic Product Professional
lauravandoore
37
7.4k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
410
Side Projects
sachag
455
43k
Chasing Engaging Ingredients in Design
codingconduct
0
270
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
650
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.3k
Transcript
⽰儘鸒鎝㻜⡲ ꤫㎗鰜!UPNNZ
䖎⛉䖎⛉⟃˘˘ ˖ 鰶鑉1PMMJOH ˖ 嫦ꥬ♧媯儘ぢ⠿剪㐼锞宠剓倞须俲 ˖ Ꟁ鰶鑉-POH1PMMJOH ˖ ⠿剪㐼莅㹐䨩畮⥃䭰鸮箁瘞䖊须俲刿倞 2
8FC4PDLFU ˖ 鸠䏞䘰 ˖ ⢪欽矦㋲ ˖ ⠿剪㐼莅㹐䨩畮〳ꦑ儘꧱ぢ彘鸒 ˖ *&♶佅䴂 3
None
8IZ4PDLFUJP ˖ ⢪欽矦㋲ ˖ 懳錒㐼♶佅䴂8FC4PDLFU儘荈⹛GBMMCBDL 5
㸞酤 npm install socket.io --save 6
&YQSFTT var express = require('express'); var app = express(); var
server = require('http').Server(app); var io = require('socket.io')(server); app.set('views', __dirname + '/views'); app.set('view engine', 'html'); app.engine('html', require('ejs').renderFile); app.get('/', function(req, res, next){ res.render('index'); }); server.listen(4000, function(){ console.log('Server started'); }); 7 䞕䲿銴 IUUQTTQFBLFSEFDLDPNUPNNZGBTUXFCEFWFMPQNFOUXJUIFYQSFTT
7JFX <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Chat room</title> </head>
<body> <script src="/socket.io/socket.io.js"></script> </body> </html> 8 鯺Ⰵ4PDLFUJPⴅ䒭䏨
姹鵔鎝䜂 9 畮 var socket = io.connect('http://localhost:4000'); socket.on('message', function(data){ alert(data.content);
}); 䖕畮 io.on('connection', function(socket){ socket.emit('message', {content: 'Hi there!'}); }); ⢪欽罏鸮箁ⵌ⠿剪㐼 ⠿剪㐼䱺佐ⵌ⢪欽罏涸鸮箁 ぢ⢪欽罏⫄鷑姹鵔鎝䜂 ⢪欽罏䱺佐ⵌ姹鵔鎝䜂
崩玑 10 使⽤用者 伺服器 ⢪欽罏鸮箁ⵌ⠿剪㐼 ぢ⢪欽罏⫄鷑姹鵔鎝䜂
1VC4VC垸䒭 ˖ 1VCMJTI4VCTDSJCF垸䒭 ˖ 㖈/PEFKT⚥⟃&WFOU&NJUUFS㻜⡲ 11 Subscriber Publisher 4VCTDSJCF PO
FWFOU 1VCMJTI FNJU FWFOU
䑞久 䑞久窍䨾剤⢪欽罏ꤑ✫荈䊹 socket.broadcast.emit('message', { content: 'Hi everyone!' }); 䑞久窍䨾剤⢪欽罏⺫䭍荈䊹 io.emit('message',
{ content: 'Hi everyone!' }); 12
翝㣔㹔 畮 var socket = io.connect('http://localhost:4000/room'); 䖕畮 io.of('/room').on('connection', function(socket){ console.log('User
connected to the room!'); }); 13
5IBOLT 14