-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp-daemon.js
More file actions
executable file
·90 lines (62 loc) · 1.77 KB
/
Copy pathtcp-daemon.js
File metadata and controls
executable file
·90 lines (62 loc) · 1.77 KB
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
#!/usr/bin/env node
var pg = require('pg')
var dbConfig = require('./config.json')
var net = require('net');
var sockets = [];
var port = 3010;
var clientId = 0;
var server = net.createServer(function(socket) {
clientId++;
socket.nickname = clientId;
sockets.push(socket);
// When client leaves
socket.on('end', function() {
// Remove client from socket array
removeSocket(socket);
});
// When socket gets errors
socket.on('error', function(error) {
console.log('Socket Error: ', error.message);
});
});
function broadcast(message) {
// If there are no sockets, then don't broadcast any messages
if (sockets.length === 0) {
return;
}
sockets.forEach(function(socket, index, array){
socket.write(message+'\n');
});
};
// Remove disconnected client from sockets array
function removeSocket(socket) {
sockets.splice(sockets.indexOf(socket), 1);
};
// Listening for any problems with the server
server.on('error', function(error) {
console.log("Error: ", error.message);
});
// Listen for a port to telnet to
// then in the terminal just run 'telnet localhost [port]'
server.listen(port, function() {
console.log("Server listening on:" + port);
});
// Postgres Notifications
pg.connect(dbConfig, function(err, client) {
if(err) {
console.log('DB Connection Error: ',err);
}
client.on('notification', function(msg) {
switch(msg.channel) {
case "upload_row":
broadcast(msg.payload);
//logtail_namespace.emit("upload_row",msg.payload)
break;
case "upload_parse":
//logtail_namespace.emit("upload_parse",msg.payload)
break;
}
});
client.query("LISTEN upload_row");
//client.query("LISTEN upload_parse");
});