im bit confuse why connection trigger multiple time. let first start with my code
main.js
let Tokens = {};
app.getOauthToken = async function (host, port, oauthcall = false) {
if (host !== undefined && port !== undefined) {
const key = host + ":" + port;
if (Tokens[key] !== undefined) {
if(oauthcall){
console.log("already auth token present (mainjs)")
await Tokens[key].getAccessToken(false);
child.send( Tokens[key] )
}
return Tokens[key];
} else {
Tokens[key] = new Token(host, port);
if (oauthcall) {
await Tokens[key].getAccessToken(false);
console.log( " auth token in mainjs ------->", Tokens[key].token)
child.send( Tokens[key] )
}
return Tokens[key];
}
}
}
child.on('message', (m) => {
console.log(" mess from child ")
if (m && m.auth) {
app.getOauthToken(m.host, m.port, m.oauthcall);
}
});
in child.js
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true });
process.on('message', (m) => {
console.log("msg from parent ------------>", m )
});
output in 1st attempt (fresh start of main.js)
mess from child
auth token in mainjs -------> { "token" :dsadaq21}
msg from parent ------------> { "token" :dsadaq21}
output in 2nd attempt ( got message in child.js 2 time on process.on "message" )
mess from child
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
output in 3rd attempt ( got message in child.js 3 time on process.on "message" )
mess from child
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
can any one tell me is this expected behaviour or im doing something wrong or its bug in nodejs note :- i haven't used any "close" method is this issue due to not using close
You do not show it, but you are loading this each time you send a message:
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true });
process.on('message', (m) => {
console.log("msg from parent ------------>", m )
});
However you trigger that process.send
also adds another process.on
event listener.
If you need to put that process.on
in a different scope from the process.send
, so that process.on
is called only once, and process.send
can be called without also executing process.on
.
Does that make it clear?
For example:
function startTestLoop() {
// Set up listener - this must only ever be executed _once_
// `process.on()` adds _another_ listener callback every time it is run.
process.on('message', (m) => {
// This code is the callback. It runs every time the event is fired.
console.log("msg from parent ------------>", m )
});
// Send message every 10 seconds - do this till the cows come home
const handle = setInterval(() =>
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true }),
10 * 1000
)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.