diff --git a/libs/remixd/src/bin/remixd.ts b/libs/remixd/src/bin/remixd.ts index 913c7ec05a..6b80867843 100644 --- a/libs/remixd/src/bin/remixd.ts +++ b/libs/remixd/src/bin/remixd.ts @@ -36,12 +36,21 @@ const ports = { } const killCallBack: Array = [] -function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => void) { +function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder, error?:Error) => void) { const socket = new WebSocket(ports[service], { remixIdeUrl: program.remixIde }, () => services[service](program.readOnly || false)) socket.start(callback) killCallBack.push(socket.close.bind(socket)) } +function errorHandler (error: any, service: string) { + const port = ports[service] + if (error.code && error.code === 'EADDRINUSE') { + console.log('\x1b[31m%s\x1b[0m', `[ERR] There is already a client running on port ${port}!`) + } else { + console.log('\x1b[31m%s\x1b[0m', '[ERR]', error) + } +} + (async () => { const { version } = require('../package.json') program.version(version, '-v, --version') @@ -76,7 +85,11 @@ function startService (service: S, callb console.log('\x1b[33m%s\x1b[0m', '[WARN] Any application that runs on your computer can potentially read from and write to all files in the directory.') console.log('\x1b[33m%s\x1b[0m', '[WARN] Symbolic links are not forwarded to Remix IDE\n') try { - startService('folder', (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => { + startService('folder', (ws: WS, sharedFolderClient: servicesList.Sharedfolder, error: any) => { + if (error) { + errorHandler(error, 'hardhat') + return false + } sharedFolderClient.setWebSocket(ws) sharedFolderClient.setupNotifications(program.sharedFolder) sharedFolderClient.sharedFolder(program.sharedFolder) @@ -85,7 +98,11 @@ function startService (service: S, callb const hardhatConfigFilePath = absolutePath('./', program.sharedFolder) + '/hardhat.config.js' const isHardhatProject = fs.existsSync(hardhatConfigFilePath) if (isHardhatProject) { - startService('hardhat', (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => { + startService('hardhat', (ws: WS, sharedFolderClient: servicesList.Sharedfolder, error: Error) => { + if (error) { + errorHandler(error, 'hardhat') + return false + } sharedFolderClient.setWebSocket(ws) sharedFolderClient.sharedFolder(program.sharedFolder) }) diff --git a/libs/remixd/src/websocket.ts b/libs/remixd/src/websocket.ts index cc3ca7b197..370dc48abb 100644 --- a/libs/remixd/src/websocket.ts +++ b/libs/remixd/src/websocket.ts @@ -9,7 +9,7 @@ export default class WebSocket { constructor (public port: number, public opt: WebsocketOpt, public getclient: () => ServiceClient) {} //eslint-disable-line - start (callback?: (ws: WS, client: ServiceClient) => void): void { + start (callback?: (ws: WS, client: ServiceClient, error?: Error) => void): void { this.server = http.createServer((request, response) => { console.log((new Date()) + ' Received request for ' + request.url) response.writeHead(404) @@ -21,9 +21,15 @@ export default class WebSocket { 65521: 'git', 65522: 'hardhat' } + + this.server.on('error', (error: Error) => { + if (callback)callback(null, null, error) + }) + this.server.listen(this.port, loopback, () => { console.log('\x1b[32m%s\x1b[0m', `[INFO] ${new Date()} ${listeners[this.port]} is listening on ${loopback}:${this.port}`) }) + this.wsServer = new WS.Server({ server: this.server, verifyClient: (info, done) => {