Node.js call parent method in class - node.js

I am trying to emit an event inside a class.
But I can't find a way to call the parent method.
Please see the comment lines to understand what I'm trying to do.
This is my VirtualChart.js
var WebSocketClient = require('websocket').client;
const EventEmitter = require("events");
class VirtualChart extends EventEmitter {
constructor() {
super();
this.client = new WebSocketClient();
this.client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
this.client.on('connect', function(connection) {
console.log('WebSocket Client Connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
this.emit("newMessage", message.utf8Data);//here is the problem
//this.parent.emit("newMessage", message.utf8Data);??????
//this.parent.parent.emit("newMessage", message.utf8Data);??????
}
});
});
this.client.connect('wss://myurl.com');
}
}
And this is my main.js
var VirtualChart = require('./VirtualChart');
var virtualChart = new VirtualChart();
virtualChart.on("newMessage", (param) => {
console.log("newMessage " + param);
});

Well, I do see one problem. The value of this in this.emit() is probably wrong because it is set by the connection.on() handler and will not point to your object. You can fix that my using an arrow function for your callback instead of a plain function.
Change this:
this.client.on('connect', function(connection) {
console.log('WebSocket Client Connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
this.emit("newMessage", message.utf8Data);
}
});
});
to this (which uses arrow functions for the callbacks):
this.client.on('connect', connection => {
console.log('WebSocket Client Connected');
connection.on('error', error => {
console.log("Connection Error: " + error.toString());
});
connection.on('close', () => {
console.log('echo-protocol Connection Closed');
});
connection.on('message', message => {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
this.emit("newMessage", message.utf8Data);
}
});
});
The arrow functions will preserve the lexical value of this. The regular function(...) callbacks will not and the value of this will be set by the caller of the callback (which knows nothing about your object).

Related

HTTP write after end NodeJS error after first request

I have a TCP server that receive messages from a IOT device. When a message is received, my TCP server send it to my REST server via HTTP. But after the first request is successfully send, when the TCP server want to send the second message, I get a write after end error.
This is my Code on the TCP server:
const net = require("net");
const server = net.createServer(onConnected);
const http = require("http");
const querystring = require('querystring');
//HTTP STUFF
const req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding("utf8");
res.on("data", (chunk) => {
console.log(`BODY: ${chunk}`);
})
res.on("end", () => {
console.log("no more data in res");
})
});
req.on("error", (err) => {
if (err.code === "ETIMEDOUT") {
console.log("problem with request: connection timeout");
} else if (err.code === "ECONNRESET") {
console.log("problem with request: read timeout");
} else {
console.error(`problem with request: ${err.message}`);
}
});
function send(postData) {
console.log("start http");
console.log("post data: " + postData);
req.write(postData, function() {
req.end();
});
};
//TCP
server.listen(PORT, HOST, function() {
console.log("Server listening on &j", server.address());
});
function onConnected(sock) {
var remoteAddress = sock.remoteAddress + ":" * sock.remotePort;
console.log("new client connected: %s", remoteAddress);
sock.on("data", function(data) {
console.log("%s says: %s", remoteAddress, data);
let tcp_message = data.toString("utf8");
let postData = querystring.stringify({
"message": tcp_message
});
console.log("tcp message " + tcp_message)
options.headers = {'Content-Length': Buffer.byteLength(postData) }
send(postData);
let mail_text = "Address: " + remoteAddress + " data: " + tcp_message;
});
sock.on("close", function(){
console.log("connection from %s closed", remoteAddress);
});
server.on("error", function(err){
console.log("connection %s error: %s", remoteAddress, err.message);
});
}

nodejs-serialport => RE-Establish connection to port after closed

Accordding to my last question SerialPort 'close' event never fire. I was unabled to detected if the COM is disconnected so I have created my own way to detect it.
I have created timestamp, and checked it with interval() every 1 sec to see if it is connected.
when it's detect the COM is unplugged I have try to re-establish the connection or re-instance port with SerialPort like you'll see inside the code below.
When it's try to reconnect I've get Error: Access denied.
There is a way to refresh or clean the cache? , because I think the server still hold the connection when isn't closed propely.
I've also tried port.close() and it's throw me out: Error: Port is not open.
var comPort = '\\\\.\\COM7',
lastDataTime,
lastresult,
count = 0,
lastDataTime,
comStatus,
error;
var port = new SerialPort(comPort, function (err) {
if (err) {
comStatus = false;
return console.log('Error: ', err.message);
}
});
const parser = port.pipe(new Readline());
port.on('open', function () {
console.log('~Port is open.');
parser.on('data', function (data) {
comStatus = true;
lastDataTime = Date.now();
if (++count == 10) {
count = 0;
lastresult = data;
}
});
});
setInterval(function () {
if (Date.now() - lastDataTime > 1000 || !comStatus) {
comStatus = false;
port.close();
port = new SerialPort(comPort, function (err) {
if (err) {
error = 'Error: ' + err.message;
return console.log(error);
}
});
}
}, 1000);
app.get('/', function (req, res) {
res.send((comStatus) ? lastresult : 'Disconnected - ' + error);
console.log(lastresult);
})
Thanks!
As you can see in /node_modules/serialport/lib/serialport.js: close-event may not be emitted (unlike disconnect).
You can add console.log locally like below to simple debug.
P.S. I tested it on Win7x32. Close-event is emitted.
SerialPort.prototype._disconnected = function(err) {
this.paused = true;
this.emit('disconnect', err);
// add: console.log('1', this.closing);
if (this.closing) {
return;
}
// add: console.log('2', this.fd);
if (this.fd === null) {
return;
}
this.closing = true;
if (process.platform !== 'win32') {
this.readable = false;
this.serialPoller.close();
}
// add: console.log('3');
SerialPortBinding.close(this.fd, function(err) {
// add: console.log('4', this._events.close.toString());
this.closing = false;
if (err) {
debug('Disconnect close completed with error: ', err);
}
this.fd = null;
this.emit('close'); // it's your target
}.bind(this));
};
Reconnect example
var SerialPort = require('serialport');
var port = new SerialPort('COM1', {autoOpen: false, baudRate: 9600});
function open () {
port.open(functon (err) {
if (!err)
return;
console.log('Port is not open: ' + err.message);
setTimeout(open, 10000); // next attempt to open after 10s
});
}
port.on('open', function() {
function send() {
if (!port.isOpen()) // v5.x require
return console.log('Port closed. Data is not sent.');
port.write(123, function (err) {
if (err)
console.log('Error on write: ' + err.message)
port.drain(() => console.log('DONE'));
});
}
setInterval(send, 1000);
});
port.on('close', function () {
console.log('CLOSE');
open(); // reopen
});
port.on('data', (data) => console.log('Data: ' + data));
port.on('error', (err) => console.error('Error: ', err.message));
open(); // open manually
According to the serialport.io,
The resume() method causes an explicitly paused, Readable stream to
resume emitting 'data' events, switching the stream into flowing mode.
Simply, when port is closes, serialport library emits a close event
serialport.on('close', function(error){
if(error.disconnected === true){
console.log("disconnected");
}
}
, which will allow us whether port is disconnected or not.
That means the disconnected port is not available to re-establish the connection again, so you have to use serialport.resume() method to re-enable the connection.
serialport.on('close', function(err){
console.log("Port closed.");
if(err.disconnected === true){
console.log("Disconnected!");
serialport.resume(function(e){
reconnectDevice(); // Serial Port Initialization Function. It's your method to declare serial port.
console.log("Error on resuming port:", e);
});
}
});
After that, it will automatically switch COM ports and you won't get error as 'Port Access denied.'.

node.js mariasql return undefined

I believe i have a problem with the Syntax.
By the Function xx the return is undefined :(.
Here the Problem in one File.
var Client = require('mariasql');
var inspect = require('util').inspect;
var c = new Client();
c.connect({
host: '127.0.0.1',
user: 'root',
password: '38nudel5nu',
db: 'artikel2'
});
var login = function(){
console.log("LOGIN\n");
c.on('connect', function() {
console.log('Client connected');
})
.on('error', function(err) {
console.log('Client error: ' + err);
})
.on('close', function(hadError) {
console.log('Client closed');
});
}
var end = function(){
console.log("EXIT");
c.end();
}
login();
var xx = function(){
c.query("SELECT COUNT(ArtikelID) AS Count FROM artikel")
.on('result', function(res) {
res.on('row', function(row) {
return "YOLO";
})
.on('error', function(err) {
})
.on('end', function(info) {
});
})
.on('end', function() {
});
}
var autohaus = xx();
console.log("\n\n --> " + autohaus);
And here is the Output:
[cseipel#myhost testumgebung]$ node skript.js LOGIN
--> undefined Client connected
You're using an asynchronous function as if it were synchronous. That's not going to work. You need to pass in a callback to your ArtikelCount function and call the callback once you have the results you want (the typical convention for callbacks is to have the first argument be an error if an error occurred, otherwise it should be null).
Example:
var ArtikelCount = function(cb) {
var count,
error;
c.query('SELECT COUNT(ArtikelID) AS Count FROM artikel')
.on('result', function(res) {
res.on('row', function(row) {
count = row.Count;
})
.on('error', function(err) {
console.log('Result error: ' + inspect(err));
error = err;
})
.on('end', function(info) {
console.log('Result finished successfully');
});
})
.on('end', function() {
console.log('Done with all results');
cb(error, count);
});
}
Then use it like:
wc.ArtikelCount(function(err, count) {
if (err)
throw err;
else
console.log('Row count', count);
});

Nodejs: This socket is closed

I am using tcp sockets in Node.js to communicate with a Java client.
See my very basic server implementation below:
var server = my_http.createServer();
echo.installHandlers(server, {
prefix: '/echo'
});
server.listen(8000, '0.0.0.0');
var socketServer = net.createServer(function (socket) {
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort
// Put this new client in the list
clients.push(socket);
sockets[socket.name] = socket;
// Handle incoming messages from clients.
socket.on('data', function (data) {
try {
var obj = JSON.parse(data);
if (obj.type == "sendMessage") {
broadcast("{\"id\":\"1\", \"msg\": \"" + obj.msg + "\", \"name\": \"" + obj.name + "\", \"time\": \"" + getDateTime() + "\"}\n", socket);
}
} catch (er) {
}
});
// Remove the client from the list when it leaves
socket.on('end', function () {
try {
clients.splice(clients.indexOf(socket), 1);
} catch (err) {
}
});
// Send a message
function broadcast(message, sender) {
try {
clients.forEach(function (client) {
client.write(message); // ERROR IS HERE
});
} catch (ee) {
}
}
return socket;
}).listen(8080);
For some reason sometimes i get this error:
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: This socket is closed.
Its happening on this line:
client.write(message); // ERROR IS HERE
Any ideas how to prevent this from happening?
or use client.writable to detect if the socket available.
clients.forEach(function (client) {
try {
if (client.writable) {
clientwrite("are u alive");
} else {
console.log('client is not writable');
}
} catch (err) {
console.log("cannot send message.");
}
}
Try this code
Option1
function broadcast(message, sender) {
try {
clients.forEach(function (client) {
if(client._handle){ // ensure there is still underlying handle
client.write(message); // ERROR IS HERE
}
});
} catch (ee) {
}
}
Option2
Attach error event listener on socket.
Socket.on('error',function(){
console.log("%j", arguments);
});

How to SSH into a server from a node.js application?

var exec = require('child_process').exec;
exec('ssh my_ip',function(err,stdout,stderr){
console.log(err,stdout,stderr);
});
This just freezes - I guess, because ssh my_ip asks for password, is interactive, etc. How to do it correctly?
There's a node.js module written to perform tasks in SSH using node called ssh2 by mscdex. It can be found here. An example for what you're wanting (from the readme) would be:
var Connection = require('ssh2');
var c = new Connection();
c.on('connect', function() {
console.log('Connection :: connect');
});
c.on('ready', function() {
console.log('Connection :: ready');
c.exec('uptime', function(err, stream) {
if (err) throw err;
stream.on('data', function(data, extended) {
console.log((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ')
+ data);
});
stream.on('end', function() {
console.log('Stream :: EOF');
});
stream.on('close', function() {
console.log('Stream :: close');
});
stream.on('exit', function(code, signal) {
console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
c.end();
});
});
});
c.on('error', function(err) {
console.log('Connection :: error :: ' + err);
});
c.on('end', function() {
console.log('Connection :: end');
});
c.on('close', function(had_error) {
console.log('Connection :: close');
});
c.connect({
host: '192.168.100.100',
port: 22,
username: 'frylock',
privateKey: require('fs').readFileSync('/here/is/my/key')
});
The other library on this page has a lower level API.
So I've wrote a lightweight wrapper for it. node-ssh which is also available on GitHub under the MIT license.
Here's an example on how to use it.
var driver, ssh;
driver = require('node-ssh');
ssh = new driver();
ssh.connect({
host: 'localhost',
username: 'steel',
privateKey: '/home/steel/.ssh/id_rsa'
})
.then(function() {
// Source, Target
ssh.putFile('/home/steel/.ssh/id_rsa', '/home/steel/.ssh/id_rsa_bkp').then(function() {
console.log("File Uploaded to the Remote Server");
}, function(error) {
console.log("Error here");
console.log(error);
});
// Command
ssh.exec('hh_client', ['--check'], { cwd: '/var/www/html' }).then(function(result) {
console.log('STDOUT: ' + result.stdout);
console.log('STDERR: ' + result.stderr);
});
});
The best way is to use promisify and async/await. Example:
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
export default async function (req, res) {
const { username, host, password } = req.query;
const { command } = req.body;
let output = {
stdout: '',
stderr: '',
};
try {
output = await exec(`sshpass -p ${password} ssh -o StrictHostKeyChecking=no ${username}#${host} ${command}`);
} catch (error) {
output.stderr = error.stderr;
}
return res.status(200).send({ data: output, message: 'Output from the command' });
}
Check my code:
// redirect to https:
app.use((req, res, next) => {
if(req.connection.encrypted === true) // or (req.protocol === 'https') for express
return next();
console.log('redirect to https => ' + req.url);
res.redirect("https://" + req.headers.host + req.url);
});
pure js/node way to ssh into hosts. Special thanks to
ttfreeman. assumes ssh keys are on host. No need for request object.
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
require('dotenv').config()
//SSH into host and run CMD
const ssh = async (command, host) => {
let output ={};
try {
output['stdin'] = await exec(`ssh -o -v ${host} ${command}`)
} catch (error) {
output['stderr'] = error.stderr
}
return output
}
exports.ssh = ssh

Resources