forked from LogIN-/ospnc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·102 lines (84 loc) · 2.93 KB
/
index.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
// Setup basic express and socket.io server
var server = {};
server.express = require('express');
server.mainApp = server.express(),
server.mainServer = require('http').createServer(server.mainApp),
server.io = require('socket.io')(server.mainServer);
// Some configs and handlers
var config = {
port: 8089,
clients: [],
client_id: null,
queue_count: 0
};
/* Application wrapper */
var ospnc = {};
// Main NameChecker scrapper class
ospnc.NameChecker = require('./lib/checker');
ospnc.func = require('./lib/functions');
// Start server on port
server.mainServer.listen(config.port, function() {
console.log('Server listening at port %d', config.port);
});
server.io.set('origins', '*:*');
// Routing for static fronted files
server.mainApp.use(server.express.static(__dirname + '/public'));
server.io.sockets.on('connection', function(socket) {
var clientId = null;
// Register for private session
socket.on("register", function(data) {
// Create new Client
clientId = data.guid;
config.clients[clientId] = {};
config.clients[clientId].socket = socket;
config.clients[clientId].requests_count = 0;
// User just opened our page lets give him unique ID and tell him that
config.clients[clientId].socket.emit('welcome', {
message: 'Welcome',
guid: clientId
});
// User has connected
socket.broadcast.emit('actions', {
action: 'registered',
data: clientId
});
});
socket.on('process', function(data) {
// Create new Client
var clientId = data.guid;
var clientSearch = data.clientSearch;
config.clients[clientId].requests_count += 1;
config.clients[clientId].debug_count = 0;
config.queue_count += 1;
// config.clients[clientId].socket.emit('system', { message: 'Your request "' + config.clients[clientId].requests_count + '" is ' + config.queue_count + ' in queue!'});
/* Check for availability */
new ospnc.NameChecker({}, clientSearch,
// On one item processed callback
function(response) {
config.clients[clientId].socket.emit('console_output', {
data: response,
guid: clientId,
clientSearch: clientSearch
});
},
// On task ended callback
function() {
config.queue_count -= 1;
});
// Emit query to public
socket.broadcast.emit('actions', {
action: 'new_query',
data: clientSearch
});
});
socket.on('disconnect', function() {
// User has disconnected
socket.broadcast.emit('actions', {
action: 'disconnected',
data: clientId
});
if(config.clients[clientId]){
delete config.clients[clientId];
}
});
});