This is follow up to the post: How to write a JSON File over TCP Socket in express to C++ program. For the latest update, I have chosen to send text over TCP port instead of JSON file
Summary
The task is that I have to listen to a changes in a website, (listening for a webhook). when the webhook is triggered, I have to resend it to be send out by the TCP server. The TCP server will parse the webhook content, extract the relevant data and resend it out to another TCP client (code not shown here)
var request = require('request'),
express = require('express'),
path = require('path');
http = require('http');
// the external parser
var sf = require('./Parse');
// configuration setting
const config = {
choice : 1,
http_port : 5000,
tcp_port : 1337,
host : '127.0.0.1'
};
///////////////////////////////////////////////////////////////////////////////////////////////////
//////// TCP SERVICE
//////////////////////////////////////////////////////////////////////////////////////////////////
const net = require('net');
var clients =[];
var tcpserver = net.createServer(function(socket) {
// Track the client
clients.push(socket);
socket.write('Echo server\r\n');
socket.write('......\r\n');
socket.write('Connected with the server \r\n');
socket.write('Waiting for communication\r\n');
socket.on('error', function(err) {
console.log(err)
});
});
tcpserver.on('connection',function(socket){
console.log('Detected connection with the client');
var rport = socket.remotePort;
var raddr = socket.remoteAddress;
var rfamily = socket.remoteFamily;
console.log('REMOTE Socket is listening at port:' + rport);
console.log('REMOTE Socket ip :' + raddr);
console.log('REMOTE Socket is IP4/IP6 : ' + rfamily);
});
tcpserver.listen(config.tcp_port, config.host, function(){
var host = tcpserver.address().address
var portid = tcpserver.address().port
console.log('TCP server listening at http://%s:%s', host, portid);
console.log("TCP server listening on port " + portid);
});
tcpserver.on('data', function(data, socket){
var decrypted = sf.parse(data);
console.log("The descrypted data is :");
console.log(decrypted);
if(config.choice == 1)
{
// send body in string/text
tcpserver.write("MSG" + ","
+ 2 + ","
+ 1 + ","
+ 1 + ","
+ 'AAAAAA' + ","
+ decrypted.Time + ","
+ decrypted.Time + ","
+ ","
+ ","
+ ","
+ ","
+ ","
+ decrypted.Latitude + ","
+ decrypted.Longitude + ","
+ decrypted.SpeedKmH + ","
+ decrypted.Heading + ","
+ ","
+ ","
+ ","
+ ","
+ ","
+ ","
+ -1
+ "\n");
// client.write(JSON.stringify(data));
}
});
//////////////////////////////////////////////////////////////////////////////////////////////////
/////// HTTP SERVICE
/////////////////////////////////////////////////////////////////////////////////////////////////
var app = express();
// for Express 4.16 above use this instead of
// using body-Parser: app.use(bodyParser.json())
app.use(express.json());
app.set('port', (process.env.PORT || config.http_port));
var client = new net.Socket();
app.post('/getSomeWebHook', function(req, res)
{
// tester code
var webdata = req.body;
var data= sf.parse(webdata);
// client.write('Hello, server! Love, Client.' + id + "\n");
broadcast(':' + data);
// to check whether its correct
res.json(JSON.stringify(req.body));
});
// http server location
var server = app.listen(app.get('port'), function() {
var host = server.address().address
var portid = server.address().port
console.log('Web App listening at http://%s:%s', host, portid)
console.log("Web App listening on port " + portid);
});
// error handler
app.use(function (err, req, res, next) {
res.status(400).send(err.message)
});
/////////////////////////////////////////////////////////////////////////////////////
////////////// HELPER
/////////////////////////////////////////////////////////////////////////////////////
// Here's where you send data to all clients
function broadcast(msg) {
console.log("broadcasting message");
clients.forEach(function (client) {
client.write(msg);
});
}
Just wondering is there anything wrong with the code... especially when seending the message... it seem to have this error
Error: This socket has been ended by the other party] code: 'EPIPE
OR how can it be improved
Thanks
REgards