-const body = document.createElement('body')
-const root = document.createElement('div')
document.title = "Strapp.io Host"
+const clients = []
if ("WebSocket" in window) {
- const wsock = new WebSocket("wss://" + window.location.host + ":" + _strapp_port)
- wsock.onopen = () => {
- console.log("Strapped to wss://" + window.location.host + ":" + _strapp_port)
- }
- wsock.onmessage = (evt) => {
- console.log("Incoming connection from " + evt.data)
- console.log("TODO: Open a socket to this client")
- }
-} else {
- root.appendChild(document.createTextNode("Your browser does not support Strapp"))
+ document.addEventListener('DOMContentLoaded', (event) => {
+ const wsock = new WebSocket("wss://" + window.location.host + ":" + _strapp_port)
+ wsock.onopen = () => {
+ console.log("Strapped to wss://" + window.location.host + ":" + _strapp_port)
+ }
+ wsock.onmessage = (evt) => {
+ console.log("Incoming connection from " + evt.data)
+ console.log("TODO: Open a socket to this client")
+ wsock.send("Got " + evt.data)
+ clients.push({
+ ip: evt.data,
+ dataChannel: undefined
+ })
+ }
+ })
+}
+else {
+ document.addEventListener('DOMContentLoaded', () => {
+ document.body.innerHTML = 'Websockets not supported in your browser'
+ })
}
-body.appendChild(root)
-document.body = body
const clientJS = fs.readFileSync(opts['client-js'])
const hostJS = fs.readFileSync(opts['host-js'])
const routes = {}
-const httpsOpts = (opts['no-tls'] ?
- undefined :
- {
- key: fs.readFileSync(opts['ca-key']),
- cert: fs.readFileSync(opts['ca-cert'])
- })
+const httpsOpts = {}
+if (!opts['no-tls']) {
+ fs.readFile(opts['ca-key'], { encoding: 'utf8' }, (err, data) => {
+ if (err) {
+ console.log(`WARN: Key ${opts['ca-key']} not accessible, tls will fail`)
+ httpsOpts.key = ''
+ }
+ else
+ httpsOpts.key = data
+ })
+ fs.readFile(opts['ca-cert'], { encoding: 'utf8' }, (err, data) => {
+ if (err) {
+ console.log(`WARN: Cert ${opts['ca-cert']} not accessible, tls will fail`)
+ httpsOpts.cert = ''
+ }
+ else
+ httpsOpts.cert = data
+ })
+}
const routeConnection = (request,response) => {
const serveFile = (fPath) => {
response.writeHead(200, { 'Content-Type': 'text/html' })
response.write(`${skelPage[0]}${clientJS}${skelPage[1]}`)
response.end()
+ //TODO: if route.socket == undefined: have server delay this send until host connects
+ // (this happens when a client connects to an active route with no currently-online host)
route.socket.send(request.headers['x-forwarded-for'] || request.connection.remoteAddress)
}
else {
- routes[htArgv[0]] = true
+ routes[routeName] = true
const newRoute = {}
newRoute.host = request.headers['x-forwarded-for'] || request.connection.remoteAddress
getport().then( (port) => {
else
newRoute.httpd = https.createServer(httpsOpts)
newRoute.httpd.listen(newRoute.port)
- newRoute.ws = new ws.Server( { server: newRoute.httpd } )
- newRoute.ws.on('connection', (ws) => { console.log("socket connected"); newRoute.socket = ws; ws.send("CONNECTED") } )
- newRoute.ws.on('message', (msg) => { console.log("Received message" + msg) })
- console.log("Listening for websocket " + newRoute.host + " on port " + newRoute.port)
- routes[htArgv[0]] = newRoute
+ newRoute.wsd = new ws.Server( { server: newRoute.httpd } )
+ newRoute.wsd.on('connection', (sock) => {
+ newRoute.socket = sock
+ sock.on('message', (msg) => { console.log(`[${newRoute.host}] ${msg}`) })
+ })
+ console.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`)
+ routes[routeName] = newRoute
}).then(() => {
response.writeHead(200, { 'Content-Type': 'text/html' })
- response.write(skelPage[0] + 'const _strapp_port = \'' + newRoute.port + '\'\n' + hostJS + skelPage[1])
+ response.write(`${skelPage[0]}const _strapp_port = ${newRoute.port}\n${hostJS}\n${skelPage[1]}`)
response.end()
})
}
bindings may be established by separating the
key:value pairs with ',' commas. (www:./www)
e.g. Service a typical frontpage
- -i./html/index.html -bjs:./js,html:./html,css:./css
+ -i ./html/index.html -b js:./js,html:./html,css:./css
e.g. Route-based http hosting
- -buser1:/home/user1/www,user2:/home/user2/www
+ -b user1:/home/user1/www,user2:/home/user2/www
-e, --electron=string Specify a route name for the local electron render
window which will be launched on execution (nil)
-d, --dedicated=string Route all incoming connections to this route (nil)
- can be used in conjunction with '-e' for single-user
e.g. Create a dedicated electron listener
- - strapp -edefault -ddefault
+ $ strapp -e default -d default
- All routes point to 'default' which is bound to
the local electron window. No other hosts may
be established, so no remote hosts may exist
long-polling and AJAX fallbacks (false)
- enables optional socket.io dependency
-(c)2017 jk software
+(c)2017 loljk