-
Notifications
You must be signed in to change notification settings - Fork 163
/
index.js
52 lines (38 loc) · 1.65 KB
/
index.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
'use strict';
const bodyParser = require('body-parser');
const browserify = require('browserify-middleware');
const express = require('express');
const { readdirSync, statSync } = require('fs');
const { join } = require('path');
const { mount } = require('./lib/server/rest/connectionsapi');
const WebRtcConnectionManager = require('./lib/server/connections/webrtcconnectionmanager');
const app = express();
app.use(bodyParser.json());
const examplesDirectory = join(__dirname, 'examples');
const examples = readdirSync(examplesDirectory).filter(path =>
statSync(join(examplesDirectory, path)).isDirectory());
function setupExample(example) {
const path = join(examplesDirectory, example);
const clientPath = join(path, 'client.js');
const serverPath = join(path, 'server.js');
app.use(`/${example}/index.js`, browserify(clientPath));
app.get(`/${example}/index.html`, (req, res) => {
res.sendFile(join(__dirname, 'html', 'index.html'));
});
const options = require(serverPath);
const connectionManager = WebRtcConnectionManager.create(options);
mount(app, connectionManager, `/${example}`);
return connectionManager;
}
app.get('/', (req, res) => res.redirect(`${examples[0]}/index.html`));
const connectionManagers = examples.reduce((connectionManagers, example) => {
const connectionManager = setupExample(example);
return connectionManagers.set(example, connectionManager);
}, new Map());
const server = app.listen(3000, () => {
const address = server.address();
console.log(`http://localhost:${address.port}\n`);
server.once('close', () => {
connectionManagers.forEach(connectionManager => connectionManager.close());
});
});