Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function onReady () {
switch (json.name) {
// App
case consts.eventNames.appCmdQuit:
rl.close()
app.quit();
break;

Expand Down Expand Up @@ -548,4 +549,4 @@ function sessionCreate(webContents, sessionId) {
module.exports = {
lastWindow,
start
}
}
62 changes: 45 additions & 17 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,51 @@ const url = require("url");

// Client can read/write messages from a TCP server
class Client {
// init initializes the Client
init(addr) {
let u = url.parse("tcp://" + addr, false, false);
this.socket = new net.Socket();
this.socket.connect(u.port, u.hostname, function() {});
this.socket.on("close", function() {
process.exit();
});
return this;
}

// write writes an event to the server
write(targetID, eventName, payload) {
let data = { name: eventName, targetID: targetID };
if (typeof payload !== "undefined") Object.assign(data, payload);
this.socket.write(JSON.stringify(data) + "\n");
}
// init initializes the Client
init(addr) {

this.connect(addr)

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()
})
return this
}

// write writes an event to the server
write(targetID, eventName, payload) {
let data = {name: eventName, targetID: targetID}
if (typeof payload !== "undefined") Object.assign(data, payload)
this.socket.write(JSON.stringify(data) + "\n")
}

/*
* for proper socket closing, unix socket remains open even after
* quiting the application, this function ends the socket connection
*/
close() {
this.socket.end()
}

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

module.exports = new Client();