Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.
1 change: 1 addition & 0 deletions 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
58 changes: 39 additions & 19 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
"use strict";

const net = require("net");
const url = require("url");
const net = require("net")
const url = require("url")
const {dialog} = require("electron")

// 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;
}
// init initializes the Client
init(addr) {

this.socket = net.createConnection(addr);

this.socket.on('error', function(err){
// Raising an exception in case of any error in socket
const messageBoxOptions = {
type: "error",
title: "Error in Main process",
message: err
};
dialog.showMessageBox(messageBoxOptions);
process.exit(1);
});

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

// write writes an event to the server
write(targetID, eventName, payload) {
if(this.socket.destroyed) return;
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();
}
}

module.exports = new Client();