mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 01:59:58 +00:00
Add snap support (#60)
* Initial app daemon implementation * Pass script path to child bridge processes * Change WebSocket server port * Fix error sending message whilst WebSocket connection is closing * Initial ext daemon connection implementation
This commit is contained in:
61
app/src/daemon/index.ts
Normal file
61
app/src/daemon/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
"use strict";
|
||||
|
||||
import { spawn } from "child_process";
|
||||
import { Readable } from "stream";
|
||||
|
||||
import path from "path";
|
||||
import WebSocket from "ws";
|
||||
|
||||
import { DecodeTransform
|
||||
, EncodeTransform } from "../transforms";
|
||||
|
||||
|
||||
const wss = new WebSocket.Server({ port: 9556 });
|
||||
|
||||
wss.on("connection", socket => {
|
||||
|
||||
/**
|
||||
* Daemon and bridge are the same binary, so spawn a new
|
||||
* version of self in bridge mode.
|
||||
*/
|
||||
const bridge = spawn(process.execPath, [ process.argv[1] ]);
|
||||
|
||||
// Stream for incoming WebSocket messages
|
||||
const messageStream = new Readable({
|
||||
objectMode: true
|
||||
});
|
||||
|
||||
// tslint:disable-next-line:no-empty
|
||||
messageStream._read = () => {};
|
||||
|
||||
/**
|
||||
* Incoming JSON messages from the extension over the
|
||||
* WebSocket connection are parsed and re-encoded to be sent
|
||||
* to bridge stdin.
|
||||
*/
|
||||
socket.on("message", (message: string) => {
|
||||
messageStream.push(JSON.parse(message));
|
||||
});
|
||||
|
||||
messageStream
|
||||
.pipe(new EncodeTransform())
|
||||
.pipe(bridge.stdin);
|
||||
|
||||
/**
|
||||
* Incoming messages from the bridge are decoded and
|
||||
* stringified and sent to the extension over the WebSocket
|
||||
* connection.
|
||||
*/
|
||||
bridge.stdout
|
||||
.pipe(new DecodeTransform())
|
||||
.on("data", data => {
|
||||
// Socket can be CLOSING here
|
||||
if (socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(JSON.stringify(data));
|
||||
}
|
||||
});
|
||||
|
||||
// Handle termination
|
||||
socket.on("close", () => bridge.kill());
|
||||
bridge.on("exit", () => socket.close());
|
||||
});
|
||||
Reference in New Issue
Block a user