-
Notifications
You must be signed in to change notification settings - Fork 0
/
old.js
213 lines (187 loc) · 6.49 KB
/
old.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// iClick2Share Node.js server
/* jshint node:true, devel:true, forin:true, noarg:true, eqeqeq:true, evil:true, bitwise:true, strict:true, undef:true, unused:true, indent:4, maxerr:50 */
(function () {
'use strict';
// Load required libraries
var nconf = require('nconf'),
winston = require('winston'),
http = require('http'),
express = require("express"),
app = require("express")(),
server = http.createServer(app),
io = require('socket.io').listen(server);
// Log less
io.set('log level', 1);
// Generate log file
winston.add(winston.transports.File, { filename: 'iClick2Share.log' });
// Load configuration
nconf.env().file('./config.json');
// Start listening on port
server.listen(nconf.get('server:port'));
// Support CORS
app.all('/', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// Serve static files
app.use(express.static(__dirname + '/browser'));
// Types of clients
var types = {
admin: connectAdmin,
support: connectSupport,
user: connectUser
};
// Use Socket.IO for communication
var marker = 0,
me = {
name: nconf.get('server:name'),
version: nconf.get('server:version'),
config: nconf.get('client')
};
winston.info('iClick2Share server started', me.version);
io.sockets.on('connection', function (socket) {
// Say hello to new client to initiate communication, and wait for response
socket
.emit('hello', me)
.once('hello', function (info) {
winston.info('hello', info.type);
var type = types[info.type];
if (!type)
this.disconnect(); // Unknown client type
else {
info.id = info.id || ++marker;
this.set('info', info, type.bind(this, info));
}
});
});
var domains = (function () {
var domains = Object.create(null);
function available() {
var storage = [];
return {
add: function (socket) {
storage.push(socket);
},
get: function () {
return storage.shift();
},
remove: function (socket) {
var i = storage.indexOf(socket);
if (i !== -1)
storage.splice(i, 1);
}
};
}
function waiting () {
var storage = Object.create(null);
return {
add: function (id, socket) {
if (!storage[id]) {
winston.info('waiting', id);
storage[id] = [socket, setTimeout(function () {
winston.info('Timed out', id);
disconnect(storage[id][0]);
delete storage[id];
}, nconf.get('server:wait'))];
}
},
get: function (id) {
var item = storage[id];
if (!item)
return null;
delete storage[id];
clearTimeout(item[1]);
return item[0];
}
};
}
return function (domain) {
var data = domains[domain];
if (!data)
domains[domain] = data = {
available: available(),
waiting: waiting(),
get: function (id) {
return this.waiting.get(id) || this.available.get();
}
};
return data;
};
})();
function connectAdmin() {
// TODO
}
// Handle connecting support operator
function connectSupport(info) {
winston.info('Connect support', info.domain, info.id);
// If user already waiting for this support session - resume
var support = this,
domain = domains(info.domain),
user = domain.waiting.get(info.id);
if (user) {
winston.info('To waiting user');
user.get('info', function (err, other) {
connectUser(other, support);
});
}
else {
// Otherwise, put in available list
domain.available.add(support);
winston.info('Available support:', info.id);
this.on('disconnect', function () {
domain.available.remove(support);
winston.info('Unavailable support:', info.id);
});
}
}
// Handle connecting user
function connectUser(info, support) {
var domain = domains(info.domain);
support = support || domain.get(info.id);
if (!support) {
winston.info('Support unavailable ', info.domain, info.id);
this.emit('bye');
}
else {
// Attach user and support
var user = this;
support.get('info', function (err, other) {
winston.info('Attaching', info.id, other.id);
// trade info
info.self = other.id;
other.self = info.id;
support.removeAllListeners('disconnect');
var wait = domain.waiting.add;
attach(support, user, info, wait);
attach(user, support, other, wait);
winston.info('Attached', info.id, other.id);
});
}
}
function attach(src, dst, info, wait) {
var release = wait.bind(null, info.self, dst);
if (src.disconnected)
release(dst);
else {
src
.emit('attached', info)
.on('message', function (payload) {
dst.send(payload);
})
.once('bye', function () {
dst
.removeAllListeners('disconnect')
.emit('bye');
disconnect(src);
})
.on('disconnect', release);
}
}
function disconnect(socket) {
winston.info('Disconnected');
socket
.removeAllListeners('disconnect')
.emit('bye');
}
})();