-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathdefault.js
122 lines (90 loc) · 2.81 KB
/
default.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
exports.install = function() {
ROUTE('GET /');
ROUTE('GET /usage/', view_usage);
ROUTE('SOCKET /', socket_homepage);
// On client side:
// new WebSocket('ws://127.0.0.1:8004/');
};
function view_usage() {
var self = this;
self.plain(F.usage(true));
}
function socket_homepage() {
var controller = this;
/*
Send message to all
@value {String or Object}
@names {String Array} :: client.id, optional - default null
@blacklist {String Array} :: client.id, optional - default null
if (names === null || names === undefined)
message send to all users
*/
// controller.send(value, names, blacklist);
/*
Close connection
@names {String Array} :: client.id, optional - default null
if (names === null || names === undefined)
close/disconnect all users
*/
// controller.close(names);
/*
Destroy websocket
*/
// controller.destroy();
/*
Get online count
return {Number}
*/
// controller.online;
/*
Find a client
@name {String}
return {Client}
*/
// controller.find(name);
// DESTROY CONTROLLER
// controller.destroy();
// ============================================================
// client.id : client identifiactor, you can modify this property, default contain random string
// client.socket : socket (internal)
// client.req : request
// client.uri : URI
// client.ip : IP
// client.session : empty object, you can modify this property
// client.user : empty object, you can modify this property
// client.query : get URL query parameters
// client.cookie(name) : value
// client.send(value) : send message
// client.close([message]) : disconnect client
controller.on('open', function(client) {
console.log('Connect / Online:', controller.online);
client.send({ message: 'Hello {0}'.format(client.id) });
controller.send({ message: 'Connect new user: {0}\nOnline: {1}'.format(client.id, controller.online) }, null, [client.id]);
// or
/*
controller.send({ message: 'Some message' }, null, function(user) {
// filter
return user.id === client.id;
});
*/
});
controller.on('close', function(client) {
console.log('Disconnect / Online:', controller.online);
controller.send({ message: 'Disconnect user: {0}\nOnline: {1}'.format(client.id, controller.online) });
});
controller.on('message', function(client, message) {
console.log(message);
if (typeof(message.username) !== 'undefined') {
var old = client.id;
client.id = message.username;
controller.send({ message: 'rename: ' + old + ', new: ' + client.id });
return;
}
// send to all without this client
message.message = client.id + ': ' + message.message;
console.log(message);
controller.send(message);
});
// How many connections are opened?
// controller.online;
}