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
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
170
AIが無かった頃の素敵な出会いの話
codmoninc
1
430
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
370
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
260
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
170
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
120
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
150
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
890
170k Jobs a Day on GKE: Scaling Mercari's CI Platform - and What's Next for AI-Native Development
junyaokabe
0
110
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
170
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
590
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
920
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
64
56k
For a Future-Friendly Web
brad_frost
183
10k
Believing is Seeing
oripsolob
1
190
Between Models and Reality
mayunak
4
390
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Testing 201, or: Great Expectations
jmmastey
46
8.2k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
56k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
330
Documentation Writing (for coders)
carmenintech
77
5.4k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
250
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