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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Federico Ravasio
October 15, 2012
Programming
160
0
Share
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
190
Concurrency vs. Parallelism 2.0 - RubyDay 2013
razielgn
3
180
Concurrency vs. Parallelism - Codelovers 2013
razielgn
3
190
Guida intergalattica per contributori Open Source
razielgn
0
58
Other Decks in Programming
See All in Programming
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
190
Claude Codeログ基盤の構築
giginet
PRO
7
3.9k
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
700
VueエンジニアがReactを触って感じた_設計の違い
koukimiura
0
150
PHPで TLSのプロトコルを実装してみる
higaki_program
0
730
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
130
iOS機能開発のAI環境と起きた変化
ryunakayama
0
140
AIと共にエンジニアとPMの “二刀流”を実現する
naruogram
0
130
安いハードウェアでVulkan
fadis
1
890
AI活用のコスパを最大化する方法
ochtum
0
370
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
110
Linux Kernelの1文字のミスで 権限昇格ができた話
rqda
0
2.3k
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
260
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.5k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.8k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Side Projects
sachag
455
43k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
120
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.6k
BBQ
matthewcrist
89
10k
Rails Girls Zürich Keynote
gr2m
96
14k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
350
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!