-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (123 loc) · 3.33 KB
/
server.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
process.on('uncaughtException', function (err) {
console.log(err);
if (err.fatal) {
process.exit(1);
}
});
Array.prototype.remove = function(e) {
var i = this.indexOf(e);
if (i != -1) {
return this.splice(i, 1);
}
return this;
}
var root = '/html',
socketio = require('socket.io'),
connect = require('connect'),
http = require('http'),
argv = require('optimist').usage('Usage: $0 --port [http_port] --secret [pass]').default('port', 8080).demand(['secret']).argv,
app = connect(),
basicAuth = require('basic-auth-connect'),
compression = require('compression');
// gzip/deflate outgoing responses
app.use(basicAuth(function(username, password) {
return (username == argv.secret || password == argv.secret);
}));
app.use(compression());
app.use(connect.static(__dirname + root));
function Channel(name, publisher) {
this.name = name;
this.publisher = publisher;
this.subscribers = [];
this.remove = function(socket) {
this.subscribers.remove(socket);
}
this.add = function(socket) {
this.subscribers.push(socket);
}
Channel.channels[this.name] = this;
}
Channel.prototype.broadcast = function(name, data) {
for (var i = 0; i < this.subscribers.length; i++) {
this.subscribers[i].emit(name, data);
}
}
Channel.each = function(callback) {
for (var key in Channel.channels) {
if (Channel.channels.hasOwnProperty(key)) {
callback(Channel.channels[key]);
}
}
}
Channel.channels = [];
var removeSubscriber = function(socket) {
console.log('removeSubscriber: ' + socket.id);
Channel.each(function(channel) {
channel.remove(socket);
});
}
var addSubscriber = function(name, socket) {
console.log('addSubscriber: ' + socket.id);
var channel = Channel.channels[name];
if (channel) {
channel.add(socket);
}
}
var removePublisher = function(publisher) {
console.log('removePublisher: ' + publisher);
var dirty = false;
Channel.each(function(channel) {
console.log('publisher: ' + channel.publisher);
if (channel.publisher === publisher) {
delete Channel.channels[channel.name];
dirty = true;
return;
}
});
return dirty;
}
var getChannels = function() {
var names = [];
Channel.each(function(channel) {
names.push(channel.name);
});
return names;
}
// WebServer handler
var server = http.createServer(app).listen(argv.port, function(request, response) {
console.log('Listening at: http://localhost:' + argv.port);
});
// Websocket server handler
var io = socketio.listen(server).on('connection', function(socket) {
var announceChannels = function() {
io.emit('channels', getChannels());
}
console.log('socket connected: ' + socket.id);
socket.emit('channels', getChannels());
socket.on('frame', function(data) {
var channel = Channel.channels[data.channel];
if (channel) {
channel.broadcast('frame', data);
} else {
console.log('channel created: ' + data.channel + ' by ' + socket.id);
new Channel(data.channel, socket.id);
announceChannels();
}
}).on('disconnect', function() {
console.log('socket disconnected: ' + socket.id);
removeSubscriber(socket);
if (removePublisher(socket.id)) {
announceChannels();
}
}).on('subscribe', function(name) {
removeSubscriber(socket);
addSubscriber(name, socket);
}).on('unsubscribe', function() {
removeSubscriber(socket);
}).on('unpublish', function() {
if (removePublisher(socket.id)) {
announceChannels();
}
});
});