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
Federico Ravasio
October 15, 2012
Programming
0
140
NodeJS + SocketIO
Lightning talk @ Codelovers.
Federico Ravasio
October 15, 2012
Tweet
Share
More Decks by Federico Ravasio
See All by Federico Ravasio
The art of Mocking
razielgn
1
170
Concurrency vs. Parallelism 2.0 - RubyDay 2013
razielgn
3
160
Concurrency vs. Parallelism - Codelovers 2013
razielgn
3
170
Guida intergalattica per contributori Open Source
razielgn
0
45
Other Decks in Programming
See All in Programming
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
550
快速入門可觀測性
blueswen
0
500
rails newと同時に型を書く
aki19035vc
5
710
Azure AI Foundryのご紹介
qt_luigi
1
210
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
26
6k
return文におけるstd::moveについて
onihusube
1
1.4k
令和7年版 あなたが使ってよいフロントエンド機能とは
mugi_uno
10
5.3k
AHC041解説
terryu16
0
410
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
180
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
710
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
1.3k
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Optimising Largest Contentful Paint
csswizardry
33
3k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
Bash Introduction
62gerente
610
210k
Navigating Team Friction
lara
183
15k
Done Done
chrislema
182
16k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Into the Great Unknown - MozCon
thekraken
34
1.6k
Site-Speed That Sticks
csswizardry
3
270
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
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!