uci-base/examples/four-in-one.mjs

113 lines
2.9 KiB
JavaScript
Raw Normal View History

import Base from '../src/base'
// const USOCKET = __dirname + '/sample.sock'
const socketfuncs = {
write: {
happy: function(packet){
return new Promise( async (resolve) => {
let res = {}
res.response=packet.data+ ' and Im a happy puppy :)'
return resolve(res)
})
},
sad: function(packet){
return new Promise( async (resolve) => {
let res = {}
res.response='Im a sad dog :('
return resolve(res)
})
}
}
}
const tcpfuncs = {
write: {
happy: function(packet){
return new Promise( async (resolve) => {
let res = {}
res.cmd='reply'
res.response=packet.data+ ' and Im a TCP happy puppy :)'
return resolve(res)
})
},
sad: function(packet){
return new Promise( async (resolve) => {
let res = {}
res.cmd='reply'
res.response='Im a TCP sad dog :('
return resolve(res)
})
}
}
}
// const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let fio = new Base({sockets:'uc#c>n,us#s>n,tc#c>t,ts#s>t,tc2#c>t', id:'four-in-one'})
// let fio = new Base({sockets:'uc#c>n,us#s>n', id:'four-in-one'})
await fio.init()
fio.s = socketfuncs
fio.st = tcpfuncs
fio.ct = {reply: packet =>{
console.log('==============Packet Displayed for TCP consumer received packets only')
console.dir(packet.response)
console.log('===========================')
}}
fio.cn = {
reply: function (packet) {
console.log('==============Replay for only Named Pipe Consumer=========')
console.dir(packet.response)
console.log('===========================')
}
}
fio.good = {
bad: function(packet){
return new Promise( async (resolve) => {
let res = {}
res.req = packet
res.cmd='good/ugly'
res.response='The Good The Bad and The Ugly'
return resolve(res)
})
},
ugly: function (packet) {
console.log('==============reply from Good Bad command =========')
console.log(packet.response)
console.log('===========================')
}
}
let packet = {}
console.log('=============sending============')
// packet = {cmd:'echo', data:'some data to echo'}
// console.log(packet)
// await fio.send(packet,'uc')
packet = {cmd:'write:happy', data:'My name is Zoe'}
console.log(packet)
//console.log(await fio.send(packet))
console.log(fio.getPacketByName('uc',await fio.send(packet)).response)
packet = {cmd:'write:sad', data:'data to write'}
console.log(packet)
await fio.send(packet)
packet = {cmd:'write:sad', data:'sent only via tcp'}
console.log(packet)
console.log(fio.getPacketByName('tc2',await fio.sendTCP(packet)))
packet = {cmd:'good:bad'}
console.log(packet)
await fio.send(packet)
process.kill(process.pid, 'SIGTERM')
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
process.kill(process.pid, 'SIGTERM')
})