Newer
Older
import {
Hunkify,
Input,
Output,
State
} from '../hunks.js'
// DEPRICATED: RMing for pipe version,
function VFP() {
this.inputs.push(dtin)
this.outputs.push(dtout)
// TODO is tackling state sets / updates / onupdate fn's
// this is hunk -> manager commune ...
let statusMessage = new State('string', 'status', 'closed')
let retryCountHandle = new State('number', 'retrycount', 3)
let resetRetryHandle = new State('boolean', 'retryreset', false)
let addressState = new State('string', 'address', '127.0.0.1')
let portState = new State('number', 'port', 2042)
this.states.push(statusMessage, retryCountHandle, resetRetryHandle, addressState, portState)
// this ws is a client,
let ws = {}
let url = 'ws://127.0.0.1:2020'
this.outbuffer = new Array()
this.init = () => {
setTimeout(startWs, 500)
retryCountHandle.set(3)
startWs()
// to actually change the value, we would do:
// resetRetryHandle.set(value)
}
let startWs = () => {
// manager calls this once
// it is loaded and state is updated (from program)
url = 'ws://' + addressState.value + ':' + portState.value
ws.onerror = (evt) => {
this.log('ws error, will reset to check')
statusMessage.set('error')
setCheck(500)
}
ws.onclose = (evt) => {
this.log('ws close')
setCheck(500)
ws.onmessage = (message) => {
// this should be a buffer
// ok, message.data is a blob, we know it's str8 up bytes, want that
// as an array
let msgAsArray = new Uint8Array(message.data)
if(debug) console.log('WS receive, as an array:', msgAsArray);
let setCheck = (ms) => {
if (checking) {
// noop
} else {
setTimeout(checkWsStatus, ms)
checking = true
let retrycount = retryCountHandle.value - 1
statusMessage.set('not connected')
retryCountHandle.set(0)
retryCountHandle.set(retrycount)
checking = false
this.log('CHECKING STATUS')
switch (ws.readyState) {
case WebSocket.CONNECTING:
break
case WebSocket.OPEN:
this.log('is open')
break
case WebSocket.CLOSING:
this.log('is closing')
break
case WebSocket.CLOSED:
this.log('is closed, retrying ...')
startWs()
break
default:
throw new Error('nonsensical result at ws readystate check for ws')
break
}
}
}
// override default change f'n
retryCountHandle.onChange = (value) => {
retryCountHandle.set(value)
setCheck(10)
}
this.loop = () => {
// something like if(ws !== null && ws.isopen)
// if we have an open port, and have bytes to send downstream,
if (ws !== null && ws.readyState === 1) {
// no buffering
if(debug) console.log('WS transmission as array', arr)

Jake Read
committed
// HERE insertion -> buffer.from() ?
if(debug) console.log("WS sending buffer", bytesOut.buffer)
if (this.outbuffer.length > 0 && !dtout.io()) {
dtout.put(this.outbuffer.shift())