Skip to content
Snippets Groups Projects
ltv-client.js 3.11 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jake Read's avatar
    Jake Read committed
    // ltv-client 
    
    'use strict'
    
    import { Plane, AutoPlot } from './panels.js'
    
    let lastRecvdIndice = 0 
    
    
    Jake Read's avatar
    Jake Read committed
    // now we can setup some canvases, 
    
    let plane = new Plane()
    
    let proxPresPlot = new AutoPlot(10, 10, 500, 200)
    let xdcrFlowPlot = new AutoPlot(10, 220, 500, 200)
    let volumePlot = new AutoPlot(10, 430, 500, 200)
    
    
    Jake Read's avatar
    Jake Read committed
    // to test these systems, the client (us) will kickstart a new process
    // on the server, and try to establish connection to it.
    console.log("making client-to-server request to start remote process,")
    console.log("and connecting to it w/ new websocket")
    // ok, let's ask to kick a process on the server,
    // in response, we'll get it's IP and Port,
    // then we can start a websocket client to connect there,
    // automated remote-proc. w/ vPort & wss medium,
    // for args, do '/processName.js?args=arg1,arg2'
    let LOGPHY = false
    jQuery.get('/startLocal/ltv-bridge.js', (res) => {
        if (res.includes('wss-addr:')) {
            let addr = res.substring(res.indexOf(':') + 2)
            if (addr.includes('ws://')) {
                console.log('starting socket to remote at', addr)
                let ws = new WebSocket(addr)
                ws.onopen = (evt) => {
                    ws.onmessage = (msg) => {
                        msg = JSON.parse(msg.data)
                        if(msg.type == "plot update"){
                            if(msg.array.length > 0){
                                lastRecvdIndice = msg.array[msg.array.length - 1][0]
                                console.log('now last r: ', lastRecvdIndice)
    
    Jake Read's avatar
    Jake Read committed
                                // msg.array = [[indice, inspState, proxPres, xdcrFlow, volume],[indice...]]   
                                // can get loose with the indice for this hello-world, 
                                for(let i = 0; i < msg.array.length; i ++){
                                    proxPresPlot.pushPt([msg.array[i][0], msg.array[i][2]])
    
    Jake Read's avatar
    Jake Read committed
                                    //xdcrFlowPlot.pushPt([msg.array[i][0], msg.array[i][3]])
                                    //volumePlot.pushPt([msg.array[i][0], msg.array[i][4]])
    
    Jake Read's avatar
    Jake Read committed
                                } 
    
    Jake Read's avatar
    Jake Read committed
                                proxPresPlot.redraw()
    
    Jake Read's avatar
    Jake Read committed
                                //xdcrFlowPlot.redraw()
                                //volumePlot.redraw()
    
    Jake Read's avatar
    Jake Read committed
                            } else {
                                console.error('zero len array return')
                            }
                        }
                    }
                    let reqData = () => {
    
    Jake Read's avatar
    Jake Read committed
                        if(ws.readyState == ws.OPEN){
                            ws.send(JSON.stringify({ 
                                type: "plot update request",
                                lastRecvdIndice: lastRecvdIndice 
                            }))
    
    Jake Read's avatar
    Jake Read committed
                            setTimeout(reqData, 200)    
    
    Jake Read's avatar
    Jake Read committed
                        } else {
                            console.error("WS Closed")
                        }
    
    Jake Read's avatar
    Jake Read committed
                    }
                    reqData() // start polling loop 
                }
                ws.onerror = (err) => {
                    console.log('sckt err', err)
                }
                ws.onclose = (evt) => {
                    console.log('sckt closed', evt)
                }
            }
        } else {
            console.error('machine link not established', res)
        }
    })