Axione-IPE-Viewer/webapp/tests/test_event_stream/test_api_netwo.js
2023-04-05 11:58:01 +02:00

42 lines
981 B
JavaScript

//import { EventSource } from 'eventsource'
var EventSource = require('eventsource')
function handleNetwo(data) {
console.log("handle data")
console.log(data)
}
function startNetwo(refimmeuble) {
const evtSource = new EventSource(encodeURI(`http://localhost:5000/eligibilite/netwo?ref_imb=${refimmeuble}`));
evtSource.onmessage = function (event) {
try {
console.log("got data")
data = JSON.parse(event.data)
handleNetwo(data)
if (data.eligDone) {
console.log("elig done, stop stream")
evtSource.close();
}
} catch (error) {
console.log("error parsing data, stop stream: ", error)
evtSource.close();
}
}
evtSource.onerror = function (event) {
console.log("in onerror stop stream: ",event)
evtSource.close();
}
}
if (process.argv.length < 3) {
console.log("Need to specify imb param (e.g IMB/33063/S/A8DA )")
process.exit(1)
}
ref_imb = process.argv[2]
startNetwo(ref_imb)