uci-socket/examples/server.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

import { Socket as uSocket, sSocket} from '../src'
import { fs } from 'mz'
// 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
;
(async () => {
// TODO dynamic import
if(TLS_KEY_PATH && TLS_CRT_PATH && TLS) {
Socket = sSocket
console.log('using TLS')
}
2018-01-20 14:30:21 -08:00
class Test extends Socket {
constructor(opts) {
super(opts)
}
async _packetProcess(packet) {
console.log('packet being processed at socket')
if (packet.cmd) return await this[packet.cmd](packet.data,packet.name)
return {error: 'no command in packet', packet: packet }
}
async doit(data,name) {
return new Promise(resolve => {
let res = {}
console.log('data sent to doit = ', data)
res.status ='success'
res.name = name
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') ]
}
options.path = true
// let test = new Test()
let test = new Test(options)
2018-01-20 14:30:21 -08:00
await test.create()
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})