-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (31 loc) · 1.01 KB
/
index.js
File metadata and controls
37 lines (31 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var _ = require("underscore");
var pageViews = {};
var socketPages = {};
app.get('/', function(req, res){
res.sendfile('templates/index.html');
});
io.on('connection', function(socket){
socket.on("hello", function(message){
if(!_.has(pageViews, message.location)){
pageViews[message.location] = {};
}
pageViews[message.location][socket.id] = message.name;
socket.emit("id", socket.id);
io.emit("who", pageViews[message.location]);
socketPages[socket.id] = message.location;
});
socket.on("disconnect", function(){
if(socketPages[socket.id] != undefined){
var page = socketPages[socket.id];
io.emit("bye", {"id": socket.id});
delete pageViews[page][socket.id];
delete socketPages[socket.id];
}
})
});
http.listen(3000, function(){
console.log('listening on *:3000');
});