uci-base/examples/socket.js

74 lines
1.6 KiB
JavaScript

/*
simple generic client sending an ack
*
*/
import Base from '../src/base'
let opts={name:'example-socket', id:'id-example-socket'}
// if nothing set will be localhost:8080
let socket = new Base(opts)
;
(async () => {
socket.on('log',ev => {
switch (ev.level) {
// case 'trace':
// case 'debug':
// case 'info':
case 'warning':
case 'error':
case 'fatal':
case 'testing':
console.log(ev.level.toUpperCase(),'\n',ev)
break
}
})
socket.registerSocket('socket-name','s','n',{ path:'named-pipe'})
socket.ready.addObserver('test-observer')
let res = await socket.init()
if (res.error) {
console.log('errors during init')
process.kill(process.pid, 'SIGTERM')
}
socket.on('example-consumer:process', (ready,packet) => {
console.log('process state event from consumer', ready, packet.name)
})
socket.on('example-consumer-2:process', (ready,packet) => {
console.log('process state event from consumer', ready, packet.name)
})
socket.ready.subscribe(ready=>{
console.log('the socket process is ready?', ready)
})
setTimeout(()=>{
socket.emit('test-observer',true)
console.log('socket test-observer set to true')
},10000)
process.once('SIGINT', function() {
console.log('Caught interrupt signal')
socket.removeAllListeners()
process.exit()
})
process.once('SIGUSR2', async () => {
socket.removeAllListeners()
process.exit()
})
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
socket.removeAllListeners()
process.kill(process.pid, 'SIGTERM')
})