Skip to content
Snippets Groups Projects
Commit 305e98e8 authored by Jake Read's avatar Jake Read
Browse files

nice pipe template

parent 06394575
No related branches found
No related tags found
No related merge requests found
......@@ -79,6 +79,7 @@ app.post('/save/systems/:file', (req, res) => {
// we also want to institute some pipes: this is a holdover for a better system
// more akin to nautilus, where server-side graphs are manipulated
// for now, we just want to dive down to a usb port, probably, so this shallow link is OK
let workers = []
app.get('/pipeHookup/:file', (req, res) => {
// we can assume that the file is a reciprocal pipe-type in our local pipes/file.js location
// we'll open that can as a spawn, can assume it's hosting a websocket (it will tell us the port?)
......@@ -98,7 +99,11 @@ app.get('/pipeHookup/:file', (req, res) => {
console.log('worker err', err)
})
piper.on('exit', (code) => {
console.log('worker exit code', code)
if(code !== 0){
console.log('worker exist with nonzero code', code)
} else {
console.log('worker exits OK')
}
})
// then this (or similar) should be all we really need to add here, and we can do local-dev of the pipes in hunks/pipes/pipename.js and pipes/pipename.js
// so, do we spawn, or do we use workers ?
......
......@@ -12,23 +12,44 @@ import {
State
} from '../hunks.js'
const STATUS_OPENING = 'opening...'
const STATUS_OPEN = 'open'
const STATUS_CLOSED = 'closed'
const STATUS_ERROR = 'error'
export default function Pipe() {
Hunkify(this)
let debug = true
let debug = false
let statusMessage = new State('string', 'status', 'closed')
let statusMessage = new State('string', 'status', STATUS_CLOSED)
let retryButton = new State('boolean', 'retry', false)
this.states.push(statusMessage, retryButton)
let echoButton = new State('boolean', 'echo', false)
this.states.push(statusMessage, retryButton, echoButton)
retryButton.onChange = (value) => {
startWsConnection()
}
echoButton.onChange = (value) => {
send(JSON.stringify({type: 'echo'}))
}
// coming merge of init and onload, however:
this.init = () => {
// force closed at startup; else program state can make us confused,
statusMessage.set(STATUS_CLOSED)
startWsConnection()
}
let ws = {}
// I think this is nearly there, just need to test the reset button
// and check that it is 'onchange' not 'change' ... ? write that down ?
// and then hide this hunk with the list-getter ... then vfpt
let startWsConnection = () => {
// hijack ajax to ask for a websocket
// only attempt reconnect if we're not already opening, or opened
if (statusMessage.value === STATUS_OPEN || statusMessage.value === STATUS_OPENING) return
// ask the server to instantiate the reciprocal process,
statusMessage.set(STATUS_OPENING)
jQuery.get('pipeHookup/pipetemplate.js', (data) => {
if (data.startup) {
console.log('serverside launched, starting client')
......@@ -36,22 +57,46 @@ export default function Pipe() {
// have data.ip and data.port
ws = new WebSocket(`ws://${data.ip}:${data.port}`)
ws.onopen = (evt) => {
console.log('vfpt opens', evt)
if (debug) console.log(this.name, 'opens')
statusMessage.set(STATUS_OPEN)
}
ws.onerror = (evt) => {
console.log('vfpt error', evt)
ws.onerror = (err) => {
if (debug) console.log(this.name, 'error', err)
statusMessage.set(STATUS_ERROR)
}
ws.onclose = (evt) => {
console.log('vfpt close', evt)
if (debug) console.log(this.name, 'closes')
statusMessage.set(STATUS_CLOSED)
}
ws.onmessage = (msg) => {
console.log('vfpt recv', msg)
if (debug) console.log(this.name, 'recvs', msg)
recv(msg)
}
} else {
console.log('pipe received non-startup response from server')
statusMessage.set(STATUS_ERROR)
}
})
}
// write ur handler,
let recv = (msg) => {
let data = JSON.parse(msg.data)
console.log('pipe template recvs in placeholder', data)
}
// send wrapper
let send = (msg) => {
if (ws && ws.readyState === 1) {
ws.send(msg)
return true
} else {
console.error('attempt to send on a closed ws')
return false
}
}
this.loop = () => {
// ws status, ws messages ...
// handle your io
}
}
......@@ -14,5 +14,21 @@ const WSS = new WebSocketServer({port: port}, () => {
WSS.on('connection', (ws) => {
console.log('ws connects')
// ...
ws.onmessage = (msg) => {
// always .data is what we sent,
let data = JSON.parse(msg.data)
// the template-writer will want to update these ...
if(data.type === 'echo'){
console.log('msg echo', msg.data)
ws.send(msg.data)
} else {
console.log('msg non echo')
console.log(msg.data)
}
}
ws.onclose = (evt) => {
// shutdown,
console.log('ws closes, pipe exiting')
process.exit()
}
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment