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
NodeJS + SocketIO
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Federico Ravasio
October 15, 2012
Programming
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
NodeJS + SocketIO
Lightning talk @ Codelovers.
Federico Ravasio
October 15, 2012
More Decks by Federico Ravasio
See All by Federico Ravasio
The art of Mocking
razielgn
1
200
Concurrency vs. Parallelism 2.0 - RubyDay 2013
razielgn
3
190
Concurrency vs. Parallelism - Codelovers 2013
razielgn
3
200
Guida intergalattica per contributori Open Source
razielgn
0
72
Other Decks in Programming
See All in Programming
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
230
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
270
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
320
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
860
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
230
Cloudflare is Agents
chimame
0
160
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.8k
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
280
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
4.4k
AIが無かった頃の素敵な出会いの話
codmoninc
1
430
FDEが実現するAI駆動経営の現在地
gonta
2
280
Featured
See All Featured
So, you think you're a good person
axbom
PRO
2
2.1k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Fireside Chat
paigeccino
42
4k
How GitHub (no longer) Works
holman
316
150k
Building the Perfect Custom Keyboard
takai
2
840
The Spectacular Lies of Maps
axbom
PRO
1
910
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
330
What's in a price? How to price your products and services
michaelherold
247
13k
Become a Pro
speakerdeck
PRO
31
6.2k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
240
Documentation Writing (for coders)
carmenintech
77
5.4k
Transcript
+
Orientato agli eventi I/O asincrono Reactor pattern Google V8 Singolo
thread
Websocket con fallback Semplice messaggistica
None
“In attesa di uno straniero...” “Stai parlando con uno straniero,
salutalo!” Straniero: asl? Tu: 98 m antarctica Straniero: o_o” [Qualcuno lascia la chat]
Server
var waiting = []; // Lista client in attesa var
couples = {}; // { // clientID: stranger, // strangerID: client // ... // }
var io = SocketIO.listen(server); // server è un server HTTP
già inizializzato io.sockets.on('connection', function(client) { // ... });
io.sockets.on('connection', function(client) { if (waiting.length == 0) { waiting.push(client); client.emit('waiting');
} else { var stranger = waiting.pop(); couples[client.id] = stranger; couples[stranger.id] = client; client.emit('partner'); stranger.emit('partner'); } // ... });
io.sockets.on('connection', function(client) { // ... client.on('message', function(message) { couples[client.id].emit('message', message);
}); // ... });
io.sockets.on('connection', function(client) { // ... client.on('disconnect', function() { if (stranger
= couples[client.id]) { stranger.emit('left'); delete couples[stranger.id]; delete couples[client.id]; } }); });
Client
var socket = io.connect(); socket.on('waiting', function() { logMessage('Waiting for a
stranger...'); }); socket.on('partner', function() { logMessage('You are now chatting with...'); }); socket.on('message', function(message) { logMessage(message); }); socket.on('left', function() { this.logMessage('Stranger has left...'); socket.disconnect(); });
var leaveConversation = function() { logMessage('You have left...'); socket.disconnect(); };
var sendMessage = function(message) { socket.emit('message', message); logMessage(message); };
Demo!