open-stage-control/app/main/server-express.js

62 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module.exports = function(settings) {
// Headless (noGui) mode
var express = require('express')(),
path = require('path'),
http = require('http'),
server = http.createServer(express),
io = require('socket.io')(),
ioWildcard = require('socketio-wildcard')(),
ipc = {}
express.get('/', function(req, res){
res.sendFile(path.resolve(__dirname + '/../browser/index-headless.html'))
})
express.get('*', function(req, res){
res.sendFile(path.resolve(__dirname + '/../browser' + req.path))
})
io.use(ioWildcard)
io.listen(server);
server.listen(settings.read('noGui'))
ipc.send = function(name,data,clientId) {
if (clientId) {
io.to(clientId).emit(name,data)
} else {
io.emit(name,data)
}
}
var bindCallbacks = function(callbacks) {
io.on('connection', function(socket) {
socket.on('*', function(e){
var name = e.data[0],
data = e.data[1]
if (callbacks[name]) callbacks[name](data,socket.id)
if ((name=='sendOsc' && data.sync!==false) || name=='syncOsc') {
// synchronize all other connected clients
socket.broadcast.emit('receiveOsc',data)
}
if (name=='sessionOpened') {
// synchronize all other connected clients
socket.broadcast.emit('stateSend',data)
}
});
});
}
console.log('Headless mode: app available at http://127.0.0.1:'+settings.read('noGui'))
return {
ipc:ipc,
bindCallbacks:bindCallbacks
}
}