Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.
4 changes: 4 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ app.on('ready',() => {
switch (json.name) {
// App
case consts.eventNames.appCmdQuit:
rl.close()
setTimeout(function(){
app.quit();
}, 100)
app.quit();
break;

Expand Down
31 changes: 28 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ const url = require('url');
class Client {
// init initializes the Client
init() {
let u = url.parse("tcp://" + process.argv[2], false, false)
this.socket = new net.Socket()
this.socket.connect(u.port, u.hostname, function() {});

this.getconnection()

this.socket.on('error', function(err){
// Writing to a file in case of error related to socket
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I'd rather throw an exception (that will be printed on the user's screen) and kill the app instead of writing to a file few people are aware of.

I don't know how Electron does it and I'm not really familiar with NodeJS, but when there's a fatal error in Electron, it prints the error in a nice window and exit the process. Could you do the same thing instead?

If this is not possible, you should only have to print the error in stderr which will then be picked up by the go side that will print it in its logs.

var fs = require('fs');
fs.appendFile("/tmp/astilectron.log", "Socket Error: "+err, function(e){
console.log("Error while writing to file:"+e);
})
process.exit()
})

this.socket.on('close', function() {
process.exit()
})
Expand All @@ -22,6 +31,22 @@ class Client {
if (typeof payload !== "undefined") Object.assign(data, payload)
this.socket.write(JSON.stringify(data) + "\n")
}

// for proper socket closing
close() {
this.socket.end()
}

// getconnection establishes connection based on underlying OS
getconnection() {
if ( os.platform() != "win32" ) {
this.socket = net.createConnection(process.argv[1]);
} else {
let u = url.parse("tcp://" + process.argv[1], false, false)
this.socket = new net.Socket()
this.socket.connect(u.port, u.hostname, function() {});
}
}
}

module.exports = new Client()