-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.simpleserver.js
174 lines (134 loc) · 3.11 KB
/
lib.simpleserver.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
var WebSocketServer = require('ws').Server;
var uuid = require('./lib.uuid');
var logger = require('./lib.logger').logger;
function simpleserver(port, options) {
var scope = this;
this.port = port;
this.wss = new WebSocketServer({port: this.port});
this.users = {};
this.count = 0; // Total number of connections, open or closed
this.ocount = 0; // Total number of identified users
this.alias = "SERVER";
if (options.alias) {
this.alias = options.alias;
}
if (options.logger) {
this.logger = options.logger;
} else {
this.logger = new logger({label:'httpserver:'+this.options.port});
}
this._version = "1.0.0";
this.options = options;
this.wss.on('connection', function(ws) {
var uid = scope.onConnect(ws);
ws.on('message', function(message) {
scope.onReceive(ws, uid, message);
});
ws.on('close', function(code, message) {
scope.onClose(ws, uid, message);
});
ws.on('error', function(code, message) {
scope.log("error");
});
});
}
/****************
ON CONNECT
****************/
simpleserver.prototype.onConnect = function(ws) {
this.count++;
this.ocount++;
var uid = uuid.v4(); //this.count; // internal uid
this.users[uid] = {
ws: ws
};
if (this.ocount % 100 == 0) {
console.log("\033[33m"+"online:\033[0m"+this.ocount);
}
//this.logger.log(this.ocount+" users online");
this.options.onConnect({
ws: ws,
uid: uid
});
return uid;
}
/****************
ON RECEIVE
****************/
simpleserver.prototype.onReceive = function(ws, uid, message) {
this.options.onReceive({
ws: ws,
uid: uid
}, message);
}
/****************
ON CLOSE
****************/
simpleserver.prototype.onClose = function(ws, uid, message) {
//this.ccount--;
this.ocount--;
delete this.users[uid];
if (this.ocount % 100 == 0) {
console.log("\033[33m"+"online:\033[0m"+this.ocount);
}
//this.logger.log(this.ocount+" users online");
this.options.onQuit({
ws: ws,
uid: uid
});
}
/****************
WS:SEND
****************/
simpleserver.prototype.send = function(uid, data) {
this.users[uid].ws.send(this.wsencode(data));
}
/****************
WS:CLOSE
****************/
simpleserver.prototype.close = function(uid) {
this.users[uid].ws.close();
}
/****************
WS:BROADCAST
****************/
simpleserver.prototype.broadcast = function(data, except) {
var i;
var j;
var l;
// make the list
var list = {};
if (except != undefined && except.length > 0) {
// clone the user list
for(var keys = Object.keys(this.users), l = keys.length; l; --l) {
list[ keys[l-1] ] = this.users[ keys[l-1] ];
}
// remove the exceptions
l = except.length;
for (j=0;j<l;j++) {
delete list[except[j]];
}
} else {
list = this.users;
}
// broadcast
for (i in list) {
try {
list[i].ws.send(data);
} catch(e) {
// failed
}
}
}
simpleserver.prototype.wsencode = function(data) {
return JSON.stringify(data);
}
simpleserver.prototype.wsdecode = function(data) {
try {
return JSON.parse(data);
} catch (e) {
this.log("Non encoded data: ",data);
return data;
}
}
exports.simpleserver = simpleserver;