forked from kevinatown/MMM-Screencast
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDialServer.js
111 lines (102 loc) · 2.72 KB
/
DialServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const dial = require("peer-dial");
const http = require('http');
const express = require('express');
const { spawn } = require('cross-spawn');
const ipc = require('node-ipc');
ipc.config.id = 'screenCastDIAL';
ipc.config.retry = 1000;
ipc.config.socketRoot = 'tmp';
ipc.config.networkHost = 'localhost';
ipc.config.appSpace = 'MMM-Screencast';
const app = express();
const server = http.createServer(app);
const PORT = 8569;
// var PORT = 8080;
const MANUFACTURER = "Kevin Townsend";
const MODEL_NAME = "DIAL Server";
var child = null;
var apps = {
"YouTube": {
name: "YouTube",
state: "stopped",
allowStop: true,
pid: null,
launch: function (launchData, config) {
var url = "http://www.youtube.com/tv?"+launchData;
child = spawn('npm', ['start', url, config.position, config.width, config.height], {
cwd: 'modules/MMM-Screencast'
})
child.stdout.on('data', function(data) {
console.log('screencast stdout: ' + data);
});
child.stderr.on('data', function(data) {
console.log('screencast stderr: ' + data);
});
child.on('close', function(code) {
console.log('closing code: ' + code);
});
}
}
};
var dialServer = new dial.Server({
corsAllowOrigins: true,
expressApp: app,
port: PORT,
prefix: "/dial",
manufacturer: MANUFACTURER,
modelName: MODEL_NAME,
launchFunction: null,
electronConfig: {},
delegate: {
getApp: function(appName){
var app = apps[appName];
return app;
},
launchApp: function(appName,lauchData,callback){
var app = apps[appName];
var pid = null;
if (app) {
app.pid = "run";
app.state = "starting";
app.launch(lauchData, dialServer.electronConfig);
app.state = "running";
}
callback(app.pid);
},
stopApp: function(appName,pid,callback){
console.log("Got request to stop", appName," with pid: ", pid);
var app = apps[appName];
if (app && app.pid == pid) {
app.pid = null;
app.state = "stopped";
ipc.connectTo('screenCastWindow',
`/${ipc.config.socketRoot}/${ipc.config.appSpace}.screenCastWindow`,
() => {
ipc.of.screenCastWindow.on('connect',() => {
ipc.of.screenCastWindow.emit('quit');
});
ipc.of.screenCastWindow.on('quit', () => {
ipc.disconnect('screenCastWindow');
});
});
child = null;
callback(true);
}
else {
callback(false);
}
}
}
});
var App = function() {
this.config = {};
this.server = http.createServer(app);
this.start = function(config) {
dialServer.electronConfig = config;
this.server.listen(PORT,function(){
dialServer.start();
console.log("DIAL Server is running on PORT "+PORT);
});
};
};
module.exports = new App();