From 4923c4d2b06cda8d1ba0710602f205e9cd3d9d5d Mon Sep 17 00:00:00 2001 From: Florrie Date: Tue, 4 Feb 2020 22:21:48 -0400 Subject: heck around with socat stuff --- players.js | 6 +++++- socat.js | 42 +++++++++++++++++++++++++++++++----------- test-socat.txt | 0 3 files changed, 36 insertions(+), 12 deletions(-) delete mode 100644 test-socat.txt diff --git a/players.js b/players.js index 07b711c..cc0006c 100644 --- a/players.js +++ b/players.js @@ -122,7 +122,8 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { playFile(file) { let path do { - path = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) + // path = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) + path = './mtui-socket-' + Math.floor(Math.random() * 10000) } while (this.existsSync(path)) this.socat = new Socat(path) @@ -260,11 +261,14 @@ module.exports.SoXPlayer = class extends Player { module.exports.getPlayer = async function() { if (await commandExists('mpv')) { + /* if (await commandExists('socat')) { return new module.exports.ControllableMPVPlayer() } else { return new module.exports.MPVPlayer() } + */ + return new module.exports.ControllableMPVPlayer() } else if (await commandExists('play')) { return new module.exports.SoXPlayer() } else { diff --git a/socat.js b/socat.js index ca317ea..8c6b9ed 100644 --- a/socat.js +++ b/socat.js @@ -1,9 +1,15 @@ // Simple interface for making a socat process and interacting with it. -// Assumes access to the `socat` command as a child process. +// Assumes access to the `socat` command as a child process; if it's not +// present, it will fall back to just writing to the specified file. const EventEmitter = require('events') const { spawn } = require('child_process') -const { killProcess } = require('./general-util') +const { killProcess, commandExists } = require('./general-util') +const { promisify } = require('util') +const fs = require('fs') +const path = require('path') + +const writeFile = promisify(fs.writeFile) module.exports = class Socat extends EventEmitter { constructor(path) { @@ -16,13 +22,15 @@ module.exports = class Socat extends EventEmitter { this.path = path } - start() { + async start() { this.stop() - this.subprocess = spawn('socat', ['-', this.path]) - this.subprocess.stdout.on('data', data => this.emit('data', data)) - this.subprocess.on('close', () => { - this.subprocess = null - }) + if (await commandExists('socat')) { + this.subprocess = spawn('socat', ['-', this.path]) + this.subprocess.stdout.on('data', data => this.emit('data', data)) + this.subprocess.on('close', () => { + this.subprocess = null + }) + } } stop() { @@ -32,10 +40,22 @@ module.exports = class Socat extends EventEmitter { } } - send(message) { + async send(message) { if (!this.subprocess) { - this.start() + await this.start() + } + if (this.subprocess) { + this.subprocess.stdin.write(message + '\r\n') + } else { + try { + await writeFile(path.resolve(__dirname, this.path), message + '\r\n') + } catch (error) { + // :shrug: We tried! + // -- It's possible to get here if the specified path isn't an actual + // device, which is the case on Linux. Writing to that file (hopefully) + // works on Windows though, which is the case we're trying to support + // here. (On Linux you should have socat installed.) + } } - this.subprocess.stdin.write(message + '\r\n') } } diff --git a/test-socat.txt b/test-socat.txt deleted file mode 100644 index e69de29..0000000 -- cgit 1.3.0-6-gf8a5