From 49b98d447b91b9a684348406b2bc481cdd673794 Mon Sep 17 00:00:00 2001 From: matteo78 Date: Sun, 9 Nov 2014 19:46:27 +0100 Subject: [PATCH] :bug: Fix EADDRINUSE errors on Linux/Mac Clean up last socket on Linux-based systems to avoid EADDRINUSE errors poping up. --- App.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/App.js b/App.js index cc6337c..d128511 100644 --- a/App.js +++ b/App.js @@ -8,6 +8,9 @@ var ipc = require('ipc'); var net = require('net'); var util = require('util'); var events = require('events'); +var path = require('path'); +var os = require('os'); +var fs = require('fs'); /** * App @@ -32,7 +35,10 @@ function App() { util.inherits(App, events.EventEmitter); module.exports = App; -App.socket = '\\\\.\\pipe\\piano-sock'; + +// create pipe on Windows and socks on Linux-based environments +App.socket = (process.platform === 'win32') ? + '\\\\.\\pipe\\piano-sock' : path.join(os.tmpdir(), 'piano.sock'); /** * Add a new window @@ -129,6 +135,18 @@ App.prototype.chainOptions = function (callback) { App.prototype.startServer = function () { var server; + if (process.platform !== 'win32') { + // try to unlink older socket if it exists, if it doesn't, + // ignore ENOENT errors + try { + fs.unlinkSync(App.socket); + } catch (e) { + if (e.code !== 'ENOENT') { + throw e; + } + } + } + server = net.createServer(function (connection) { connection.on('data', function (data) { this.windows[0].focus();