-
Notifications
You must be signed in to change notification settings - Fork 0
/
envisalink.js
69 lines (54 loc) · 1.85 KB
/
envisalink.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
// Load the TCP Library
net = require('net');
// Keep track of the chat clients
var clients = [];
// Start a TCP Server
net.createServer(function (socket) {
// Identify this client
socket.setEncoding('utf8');
socket.name = socket.remoteAddress + ":" + socket.remotePort
// Put this new client in the list
clients.push(socket);
// Send login prompt to client
socket.write("Login: \r\n");
//broadcast(socket.name + " joined the chat\n", socket);
// Handle response from client
socket.on('data', function(data) {
console.log(data)
if(data.substr(0,1)!='%'){socket.write('OK \r\n');}
});
// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
broadcast(socket.name + " left the chat.\n");
//Stop broadcasting the envisalink commands if there is no client to send it to.
if(clients.length==0){myStopFunction()}
});
socket.on('error', function() {
clients.splice(clients.indexOf(socket), 1);
broadcast(socket.name + " left the chat.\n");
//Stop broadcasting the envisalink commands if there is no client to send it to.
if(clients.length==0){myStopFunction()}
});
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
//process.stdout.write(message)
}
if(clients.length>=1){
var myVar = setInterval(function(){broadcast("%00,01,1C08,08,00, MCKAY'S SYSTEM Ready to Arm $\r\n")}, 3000);
}
else{
myStopFunction();
}
function myStopFunction() {
clearInterval(myVar);
}
}).listen(4025);
// Put a friendly message on the terminal of the server.
console.log("Envisalink Emulator Listening on port 4025\n");