This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Handling unix socket connection based on OS #21
Open
shrey-gang
wants to merge
9
commits into
asticode:master
Choose a base branch
from
shrey-gang:unixDomainSocket
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2eddfa1
Handling unix socket connection based on OS
shreyionos 7ddc566
removed some unused code lines, rectified connection argument
shrey-gang 0e30f0a
rectified argv indices
shrey-gang c82ed56
Merge branch 'master' into unixDomainSocket
shrey-gang 7473e46
added rl.close() for better handling of socket in case of unix-domain…
shrey-gang 7b30523
added proper handling of connection address based on the string tcp/unix
shrey-gang c3458f6
changes to check the type of connection in client.js
shrey-gang b5d06be
removed client check for TCP/Unix using single function net.createCon…
shrey-gang 7d26d2c
Merge branch 'master' into unixDomainSocket
shrey-gang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
shrey-gang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * quiting the application, this function ends the socket connection | ||
| */ | ||
| close() { | ||
| this.socket.end(); | ||
| } | ||
| } | ||
|
|
||
| module.exports = new Client(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.