-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
57 lines (50 loc) · 2.82 KB
/
app.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
//----------------------------------------------------------------------------------------------------------------------
// Config
//----------------------------------------------------------------------------------------------------------------------
_ = require('underscore'),
NET = require('net'),
SOCKET = require('./app/socket.js'),
AUTH = require('./app/auth.js'),
PORT = 7200,
clients = 0;
//----------------------------------------------------------------------------------------------------------------------
// Create server
//----------------------------------------------------------------------------------------------------------------------
var server = NET.createServer(function (client) {
clients++;
var clientId = clients;
//------------------------------------------------------------------------------------------------------------------
// Client connected
//------------------------------------------------------------------------------------------------------------------
SOCKET.debug('Client connected: ' + clientId);
//------------------------------------------------------------------------------------------------------------------
// Ask for authentication
//------------------------------------------------------------------------------------------------------------------
SOCKET.send(client, {'action': 'welcome'});
//------------------------------------------------------------------------------------------------------------------
// On error event
// - Avoid node crash
//------------------------------------------------------------------------------------------------------------------
client.on('error', function (err) {
SOCKET.debug('Client disconnected: ' + clientId);
});
//------------------------------------------------------------------------------------------------------------------
// On client disconnect
//------------------------------------------------------------------------------------------------------------------
client.on('end', function () {
SOCKET.debug('Client disconnected: ' + clientId);
});
//------------------------------------------------------------------------------------------------------------------
// On receive data
//------------------------------------------------------------------------------------------------------------------
client.on('data', function (data) {
SOCKET.data(client, data);
});
client.pipe(client);
});
//----------------------------------------------------------------------------------------------------------------------
// Initialize server
//----------------------------------------------------------------------------------------------------------------------
server.listen(PORT, function () {
SOCKET.debug('Server started on port ' + PORT);
});