actual/packages/desktop-electron/findOpenSocket.js

37 lines
854 B
JavaScript
Raw Normal View History

2022-04-29 02:44:38 +00:00
const net = require('net');
const os = require('os');
const { join } = require('path');
const ipc = require('node-ipc');
ipc.config.silent = true;
function isSocketTaken(name, fn) {
return new Promise((resolve, reject) => {
ipc.connectTo(name, () => {
ipc.of[name].on('error', () => {
ipc.disconnect(name);
resolve(false);
});
ipc.of[name].on('connect', () => {
console.log('connected');
ipc.disconnect(name);
resolve(true);
});
});
});
}
async function findOpenSocket() {
let currentSocket = 1;
while (await isSocketTaken('actual' + currentSocket)) {
currentSocket++;
console.log('checking', currentSocket);
}
let socketName = 'actual' + currentSocket;
console.log(`Listening... (${socketName})`);
return socketName;
}
module.exports = findOpenSocket;