« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'index.js')
-rwxr-xr-xindex.js45
1 files changed, 44 insertions, 1 deletions
diff --git a/index.js b/index.js
index 7632844..fea9222 100755
--- a/index.js
+++ b/index.js
@@ -9,6 +9,13 @@ import Backend from './backend.js'
 import setupClient from './client.js'
 import TelnetServer from './telnet.js'
 
+import {
+  makeSocketServer,
+  makeSocketClient,
+  attachBackendToSocketClient,
+  attachSocketServerToBackend,
+} from './socket.js'
+
 import {CommandLineInterface} from 'tui-lib/util/interfaces'
 import * as ansi from 'tui-lib/util/ansi'
 
@@ -52,6 +59,9 @@ async function main() {
 
     'player-options': {type: 'series'},
     'stress-test': {type: 'flag'},
+    'socket-client': {type: 'value'},
+    'socket-name': {type: 'value'},
+    'socket-server': {type: 'value'},
     'telnet-server': {type: 'flag'},
     'skip-config-file': {type: 'flag'},
     'config-file': {type: 'value'},
@@ -113,7 +123,10 @@ async function main() {
   const { appElement, dirtyTerminal, flushable, root } = await setupClient({
     backend,
     screenInterface: new CommandLineInterface(),
-    writable: process.stdout
+    writable: process.stdout,
+    appConfig: {
+      showPartyControls: !!(options['socket-server'] || options['socket-client'])
+    },
   })
 
   appElement.on('quitRequested', () => {
@@ -164,6 +177,36 @@ async function main() {
     appElement.attachAsServerHost(telnetServer)
   }
 
+  let socketClient
+  let socketServer
+  if (options['socket-server']) {
+    socketServer = makeSocketServer()
+    attachSocketServerToBackend(socketServer, backend)
+    socketServer.listen(options['socket-server'])
+
+    socketClient = makeSocketClient()
+    socketClient.socket.connect(options['socket-server'])
+  }
+
+  if (options['socket-client']) {
+    socketClient = makeSocketClient()
+    const [ p1, p2 ] = options['socket-client'].split(':')
+    const host = p2 && p1
+    const port = p2 ? p2 : p1
+    socketClient.socket.connect(port, host)
+  }
+
+  if (socketClient) {
+    attachBackendToSocketClient(backend, socketClient)
+
+    let nickname = process.env.USER
+    if (options['socket-name']) {
+      nickname = options['socket-name']
+    }
+    backend.setPartyNickname(nickname)
+    backend.announceJoinParty()
+  }
+
   if (options['stress-test']) {
     await loadPlaylistPromise