NodeJS TCP server random not responding - node.js

I'm listening all the connections from a GPS device who is send the data to a our server. So I create a small NodeJs app to retrieve that information.
var net = require('net');
var server = net.createServer(function(connection) {
connection.on('drain', function(){
console.log('drain');
});
connection.on('data', function(data){
var dataGps = data.toString('ascii');
var fecha = new Date();
console.log(String(fecha));
var dataArray = dataGps.split(',');
});
connection.on('end', function() {
//console.log('Client disconnected'.magenta);
});
connection.on('error', function(err) {
//console.log("Connection error: " + err+" ... ".red);
//console.log('Closing connection....'.red);
//connection.destroy();
});
connection.on('close', function(){
console.log('closed event fired');
});
});
server.on('connection', function(){
console.log('new connection');
});
server.getConnections(function(err, count){
if (err) {
console.log('# Error: '+err);
}
console.log('Count: '+count);
});
server.listen(3001, function() {
console.log('Server is listening on port 3001...');
});
To keep running this App, I'm using pm2 (pm2 website).
Everything is fine except that sometimes this app becomes inactive and don't do anything until I restart it with pm2.
This app is in Amazon EC2 (t2.medium instance) Ubuntu 16.04.
I don't know what I'm doing wrong. Any help would be really helpful.

Related

Forward Android TCP Connection to HTTP proxy in Node.js

My plan is to create a proxy of my phone 4g connection.
I've made a direct tcp connection of my phone to my pc via nodejs.
I create a client.js on my phone and server.js on my pc. They connect.
Now i need to 'transform' this direct connection into a proxy, but i dont know how.
Any help i would aprecciate.
I will show my server.js and client.js code below.
Server.js
var net = require('net');
var tcpServerPort = 7000;
// a simple TCP server for testing
var server = net.createServer(function (socket) {
console.log('Client connected to server');
socket.on('close', function () {
console.log('Client disconnected from server');
});
socket.on('data', function (buffer) {
// 'echo' server
socket.write(buffer);
});
socket.on('error', function (err) {
console.log('Error: ' + err.soString());
});
});
server.listen(tcpServerPort);
Client.js
const net = require('net');
const client = new net.Socket();
const port = 7000;
const host = 'my home ip';
client.connect(port, host, function() {
console.log('Connected');
client.write("Hello From Client " + client.address().address);
});
client.on('data', function(data) {
console.log('Server Says : ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});

Actionscript xmlsocket doesn't read data which is write by the nodejs net module server. but server read the data correctly. any solution?

Node js net module server code:
var net = require('net');
var server = net.createServer(function (connection) {
console.log('client connected');
connection.on('data', function (data) {
console.log('data from flash = ' + data);
var jsonData = {};
jsonData.message = "joined";
var d = JSON.stringify(jsonData);
connection.write(d);
});
connection.on('end', function () {
console.log('client disconnected');
});
// connection.pipe(connection);
});
server.listen(3055, function () {
console.log('server is listening');
});
Action script code
this.login_socket.connect(this.server_ip,3055);
this.login_socket.addEventListener(Event.CONNECT,this.login_socket_onConnection);
this.login_socket.addEventListener(DataEvent.DATA,this.login_onData);
this.login_socket.addEventListener(IOErrorEvent.IO_ERROR,this.login_socket_onIOErrorEvent);
this.login_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.login_socket_SecurityErrorEvent);
this.login_socket.addEventListener(Event.CLOSE,this.login_socket_closeErrorEvent);
Can anyone please tell me how to use xml socket with node js net module? I have tried everything but this doesn't work at all. I want to create a socket connection for a flash game to the server. I am using laravel as backend. If anyone knows how to create it with php tell me. Thank you.
var net = require('net');
var server = net.createServer(function (connection) {
console.log('client connected');
connection.setEncoding("utf8");
// on receive data from client
connection.on('data', function (data) {
console.log('data from flash = ' + data);
var d = JSON.stringify({
message: "joined"
}) + "\0";
connection.write(d);
});
// on connection end
connection.on('end', function () {
console.log('client disconnected');
});
// on error
connection.on("error", function (exception) {
console.log('an error occurred: ' + exception);
});
});
server.listen(3055, function () {
console.log('server is listening');
});
I change nodejs server code to above code and it works fine now.

Node.js send Server input to Client. Warning about memory leak. What is my mistake? And how does node.js run code?

Server:
var net = require('net');
var stdin = process.openStdin();
var client_list = [];
var server = net.createServer(function(connection) {
//console.log('client connected');
connection.on('error', function(e){
if(e.code == 'ECONNRESET'){
console.log('Client dissconeccted');
}
});
//connection.write('Hello World!\r\n');
stdin.addListener("data", function(d) {
// note: d is an object, and when converted to a string it will
// end with a linefeed. so we (rather crudely) account for that
// with toString() and then trim()
console.log("you entered: [" + d.toString().trim() + "]");
connection.write(d.toString().trim());
});
connection.pipe(connection);
});
server.listen(9999, function() {
console.log('server is listening');
});
Client:
var net = require('net');
var HOST = 'localhost';
var PORT = 9999;
//var client = new net.Socket();
var client = net.connect(PORT, HOST, function(){
console.log('connected to server! ' + HOST + ':' + PORT);
//client.write('I am Superman');
});
client.on('data', function(data) {
var data = data.toString();
console.log(data);
//If data starts with JS add injection functionallity
if (data === "END"){
client.end();
console.log("ENDING!")
}
else if (data === "poo"){
console.log("HOLY SHIT!")
}
});
//Keep trying to connect!
client.on('error', function(e) {
console.log('Parent connection error');
//client.end();
client.connect(PORT, HOST);
});
client.on('end', function() {
console.log('disconnected from server');
});
/*var client = net.connect({port: 8080}, function() {
console.log('connected to server!');
});*/
So what happens is that it keeps adding listeners(?) and warns me at 11 listeners with the message:
"Possible EventEmitter memory leak detected. 11 data listeners added.
Use emitter.setMaxListeners() to increase limit".
Why is this? I have tried fixing this by moving stdin.addListener() but it either doesn't take the input at all or the problem persists. Am I onto something? And how is code run in node, is it a loop?
Thanks in advance!
Have run both the client and server scripts. I can't reproduce the error message that you're getting. I'm running the code on Ubuntu - nodejs 6.9.5, npm 5.4.2. Could you post the contents of your package.json file?
Update: had a look online. seems like a known old bug in Node. https://github.com/nodejs/node-v0.x-archive/issues/5108

Node.js express websocket not broadcasting to all connected clients

Im trying to create a node websocket for messaging and broadcasting using openshift. Below is my code.
var WebSocketServer = require('ws').Server;
var http = require('http');
var ipaddr = opneshift_ip;
var port = openshift_port;
var server = http.createServer();
var wss = new WebSocketServer({server: server, path: '/connectserv'});
wss.broadcast = function(data) {
for(var i in this.clients) {
console.log(this.clients[i]);
this.clients[i].send(data);
}
};
wss.on('connection', function(ws) {
console.log('a client connected');
ws.on('message', function(data) {
console.log('>>> ' + data);
ws.send('got '+data);
if (data == 'broadcst') {
console.log('broadcst');
wss.broadcast('Hi All');
}
});
ws.on('close', function() {
console.log('Connection closed!');
});
ws.on('error', function(e) {
console.log(e);
});
});
console.log('Listening at IP ' + ipaddr +' on port '+port);
server.listen(port,ipaddr);
When any client connects, console writes "a client connected".
When any client sends message, console writes ">>> message" and im getting the same at client as well ("got message")
But when multiple clients are connected, if i want to broadcast a message to all connected clients, i send "broadcst" as message. Than goes into
if (data == 'broadcst') {
console.log('broadcst');
wss.broadcast('Hi All');
}
But only the client which sends get the message.
How to make all clients to get the message?
Does each client creates separate session?
How to use redis here?
Any quick help appreciated.
Thanks.
Try
wss.broadcast = function(data) {
for(var i in wss.clients) {
console.log(wss.clients[i]);
wss.clients[i].send(data);
}
};
broadcasting with wss

How to receive log from SRCDS in node.js

I have srcds (source dedicated server)
at console add logaddress_add 0.0.0.0:25001
this turn on sending the log to the remote server
tried to catch the log in this way
var net = require('net');
var server = net.createServer(function(c) {
c.on('end', function() {
console.log('server disconnected');
});
c.pipe(c);
});
server.listen(25001);
and that
var net = require('net');
var client = net.connect({port: 25001});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
and that
var s = dgram.createSocket('udp4');
s.bind(25001, function(data) {
console.log(data)
});
no result. can someone help?
thanks in advance
[solved]
at SRCDS server
logaddress_add 0.0.0.0:8006 //for local ip
at app.js
var dgram = require('dgram'),
server = dgram.createSocket('udp4');
server.on('message', function (message, rinfo) {
var msg = message.toString('ascii').slice(5,-1);
console.log(msg);
});
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening ' + address.address + ':' + address.port);
});
server.bind(8006);
I ended up writing a little library to do this (srcds-log-receiver), which validates the packet format, extracts out the date and allows you to use the sv_logsecret function to put a small amount of authentication on the connection, since UDP packets are easily forged.
I also wrote a parser to turn those log lines in to useful objects.

Resources