96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
import { Socket as uSocket} from '../src'
|
|
|
|
// made key cert into module that also uses environment variables
|
|
// const TLS = process.env.TLS || false
|
|
// const TLS_DIR = process.env.TLS_DIR || '/opt/certs'
|
|
// const TLS_NAME = process.env.TLD_NAME || 'wc.kebler.net'
|
|
// const TLS_KEY_PATH = process.env.TLS_KEY_PATH || `${TLS_DIR}/${TLS_NAME}.key`
|
|
// const TLS_CRT_PATH = process.env.TLS_CRT_PATH || `${TLS_DIR}/${TLS_NAME}.crt`
|
|
|
|
let Socket = uSocket
|
|
|
|
class Test extends Socket {
|
|
constructor(opts) {
|
|
super(opts)
|
|
}
|
|
|
|
async doit(packet) {
|
|
return new Promise(resolve => {
|
|
let res = {}
|
|
console.log('command doit sent with data = ', packet.data)
|
|
res.status ='success'
|
|
res.cmd = 'reply'
|
|
res.data = 'this would be response data from socket doit function'
|
|
resolve(res)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
// const options = {
|
|
// tls: TLS,
|
|
// key: await fs.readFile(TLS_KEY_PATH),
|
|
// cert: await fs.readFile(TLS_CRT_PATH),
|
|
// // This is necessary only if using client certificate authentication.
|
|
// // requestCert: true,
|
|
// // This is necessary only if the client uses a self-signed certificate.
|
|
// // ca: [ fs.readFileSync('client-cert.pem') ]
|
|
// }
|
|
|
|
let options = {path:true}
|
|
// options.conPacket = {cmd:'onconnect', data:'this is a packet data sent consumer after handshake/authentification'}
|
|
options.tokens = ['cheetos']
|
|
let test = new Test(options)
|
|
|
|
async function processor (packet) {
|
|
// console.log('packet being processed at socket', packet)
|
|
if (packet.cmd) {
|
|
if (this[packet.cmd]) return await this[packet.cmd](packet)
|
|
else {
|
|
console.log('no processing function for command', packet.cmd)
|
|
return {error: 'command has no processing function', packet: packet }
|
|
}
|
|
}
|
|
console.log('no command in packet', packet)
|
|
return {error: 'no command in packet', packet: packet }
|
|
}
|
|
|
|
test.registerPacketProcessor(processor)
|
|
|
|
|
|
;
|
|
(async () => {
|
|
// TODO dynamic import
|
|
// if(TLS_KEY_PATH && TLS_CRT_PATH && TLS) {
|
|
// Socket = sSocket
|
|
// console.log('using TLS')
|
|
// }
|
|
|
|
// test.addTokens('cheetos')
|
|
await test.create()
|
|
|
|
let count = 0
|
|
// const push = setInterval( () => {
|
|
// count++
|
|
// test.push({cmd:'pushed', count:count, status:`some pushed data ${count}`})
|
|
// if (count >3) {
|
|
// clearInterval(push)
|
|
// test.push({cmd:'pushed',status:'now will simulate server going offline by stopping to send pingfor 10 seconds'})
|
|
// test.disablePing()
|
|
// setTimeout( () => {
|
|
// test.enablePing()
|
|
// },10000)
|
|
//
|
|
// }
|
|
// },3000)
|
|
|
|
|
|
// setTimeout( () => {
|
|
// console.log('closing server')
|
|
// test._destroy()
|
|
// },20000)
|
|
|
|
})().catch(err => {
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
})
|