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
130
An Introduction to Node.js
tommy351
1
350
Other Decks in Programming
See All in Programming
変化を抱擁するドキュメントの作り方 - ビジネスルール駆動開発がもたらす、コードとの新しい関係
ioki
2
100
型解析で実現する Go の言語内 DSL / Conference に Go! タイムテーブルの歩き方 for Gophers
mazrean
0
120
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
330
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
130
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
490
Flow は今どうなっているか
mizdra
PRO
0
880
AI に Inclusive UI を書かせよう — Design Rules Skill で Compose UI を作り直す
theoriatec2024
1
430
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
420
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
200
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.4k
週末にAI-DLCを本気で回したら$1,600溶けた
hbashimizu
0
120
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.5k
Featured
See All Featured
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
290
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
The Curious Case for Waylosing
cassininazir
1
490
Evolving SEO for Evolving Search Engines
ryanjones
0
280
Building a Scalable Design System with Sketch
lauravandoore
463
34k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
570
Being A Developer After 40
akosma
91
590k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
440
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