-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleserver.js
More file actions
181 lines (138 loc) · 3.33 KB
/
simpleserver.js
File metadata and controls
181 lines (138 loc) · 3.33 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
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
var WebSocketServer = require('ws').Server;
var uuid = require('./uuid');
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;
}
this._version = "1.0.0";
this.options = options;
this.log("UUID", uuid.v4());
this.log("version "+this._version);
this.log("running on port "+this.port);
this.wss.on('connection', function(ws) {
var uid = scope.onConnect(ws);
// uid is a UUID@v4
//var uid = uuid.v4(); //scope.count;
ws.on('message', function(message) {
scope.onReceive(ws, uid, scope.wsdecode(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
};
this.log("Incoming connection (#"+uid+")");
this.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];
this.log("#"+uid+" quit.");
this.log(this.ocount+" users online");
this.options.onQuit({
ws: ws,
uid: uid
});
}
/****************
WS:SEND
****************/
simpleserver.prototype.send = function(uid, data) {
//this.log("Sending to "+uid, data);
this.users[uid].ws.send(this.wsencode(data));
}
/****************
WS:CLOSE
****************/
simpleserver.prototype.close = function(uid) {
this.log("Closing connection to client #"+uid);
this.users[uid].ws.close();
}
/****************
WS:BROADCAST
****************/
simpleserver.prototype.broadcast = function(data, except) {
this.log("Broadcasting ", data);
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) {
list[i].ws.send(this.wsencode(data));
}
}
simpleserver.prototype.log = function(data, data2) {
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<"+this.alias+">");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
}
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;