-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
209 lines (165 loc) · 4.6 KB
/
Copy pathserver.js
File metadata and controls
209 lines (165 loc) · 4.6 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// set up ========================
var express = require('express');
var app = express(); // create our app w/ express
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var sys = require('sys');
const OBSWebSocket = require('obs-websocket-js');
const obs = new OBSWebSocket();
connectToObs();
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(express.static("public"));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
var availableScenes = [];
var activeRotation = [];
var transitionList = [];
var interval = 3000;
var reconnectInterval = 5000;
var timerObj;
var reconnectTimer;
var index = 0;
var connected = false;
var live = false;
const defaultTransition = 'default'
router.put('/interval', function (req, res) {
if(!isNaN(req.body.interval))
{
interval = req.body.interval;
clearInterval(timerObj);
timerObj = setInterval(updateScene, interval);
console.log("Interval set to " + interval)
res.status(204).end();
}
else
{
res.status(500).end();
}
});
router.put('/toggle', function (req, res) {
var found = false;
var element = req.body.toggle;
console.log(element);
availableScenes.forEach(function(scene){
if(scene === element)
{
var index = availableScenes.indexOf(scene);
availableScenes.splice(index, 1);
activeRotation.push(element);
found = true;
res.status(200).end();
}
});
if(!found){
activeRotation.forEach(function(scene){
if(scene === element)
{
var index = activeRotation.indexOf(scene);
activeRotation.splice(index, 1);
availableScenes.push(element);
res.status(204).end();
return;
}
});
}
res.status(500).end();
});
router.get('/interval', function(req, res) {
res.json(interval);
});
router.get('/active', function(req, res) {
res.json(activeRotation);
});
router.get('/available', function(req, res) {
res.json(availableScenes);
});
router.get('/reload', function(req, res) {
reloadScene();
res.status(200).end();
});
router.get('/connectionStatus', function(req, res) {
res.json({connectedToObs: connected, conntectedToTwitch: live});
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('OBS Rotator started on: ' + port);
// You must add this handler to avoid uncaught exceptions.
obs.on('error', err => {
console.error('socket error:', err);
});
obs.onConnectionOpened(() => {
clearInterval(reconnectTimer);
console.log('OBS Connection Opened');
connected = true;
reloadScene();
reloadTransitions();
timerObj = setInterval(updateScene, interval);
});
obs.onConnectionClosed(() => {
console.log('OBS Connection Close');
connected = false;
// start reconnect timer
setTimeout(connectToObs, 1500);
});
obs.onStreamStarted(() => {
console.log('OBS is live');
live = true;
});
obs.onStreamStopped(() => {
console.log('OBS is not live anymore');
live = false;
});
function connectToObs() {
obs.connect({ address: '127.0.0.1:4444'})
.catch(err => { // Promise convention dicates you have a catch on every chain.
console.log(err);
});
}
function updateScene() {
if(index >= activeRotation.length)
index = 0;
if(0 == activeRotation.length)
{
console.log("No Scenes in active pool");
return;
}
const newSceneName = activeRotation[index];
var transitionName = defaultTransition;
if(0 <= transitionList.indexOf(newSceneName))
{
transitionName = newSceneName;
}
// First Set Transition
obs.setCurrentTransition({'transition-name': transitionName}, (err, data) => {
// Switch Scene with new transition
obs.setCurrentScene({'scene-name': newSceneName}, (err, data) => {
console.log('Set Scene ' + newSceneName);
index++;
});
});
}
function reloadScene() {
// Send some requests.
obs.getSceneList({}, (err, data) => {
//var obj = JSON.parse(data);
availableScenes = [];
activeRotation = [];
activeRotation = data.scenes.map(scene => scene.name);
});
return;
}
function reloadTransitions() {
// Send some requests.
obs.getTransitionList({}, (err, data) => {
transitionList = data.transitions.map(transition => transition.name);
});
return;
}