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
180
Concurrency vs. Parallelism 2.0 - RubyDay 2013
razielgn
3
170
Concurrency vs. Parallelism - Codelovers 2013
razielgn
3
180
Guida intergalattica per contributori Open Source
razielgn
0
50
Other Decks in Programming
See All in Programming
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
190
パスタの技術
yusukebe
1
400
CSC305 Summer Lecture 12
javiergs
PRO
0
120
書き捨てではなく継続開発可能なコードをAIコーディングエージェントで書くために意識していること
shuyakinjo
1
310
コーディングは技術者(エンジニア)の嗜みでして / Learning the System Development Mindset from Rock Lady
mackey0225
2
580
AIでLINEスタンプを作ってみた
eycjur
1
200
「リーダーは意思決定する人」って本当?~ 学びを現場で活かす、リーダー4ヶ月目の試行錯誤 ~
marina1017
0
240
The state patternの実践 個人開発で培ったpractice集
miyanokomiya
0
150
Claude Codeで挑むOSSコントリビュート
eycjur
0
180
兎に角、コードレビュー
mitohato14
0
150
『リコリス・リコイル』に学ぶ!! 〜キャリア戦略における計画的偶発性理論と変わる勇気の重要性〜
wanko_it
1
600
Laravel Boost 超入門
fire_arlo
1
130
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
45
7.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Building Applications with DynamoDB
mza
96
6.6k
The Cult of Friendly URLs
andyhume
79
6.5k
Writing Fast Ruby
sferik
628
62k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
How to Think Like a Performance Engineer
csswizardry
25
1.8k
For a Future-Friendly Web
brad_frost
179
9.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
110
20k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
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!