change eventEmitter to decorate

develop
HerrHase 3 years ago
parent e48f93abd1
commit dab0d4686a

@ -18,7 +18,8 @@ import { EventEmitter } from 'events'
// create eventemitter for sending messages // create eventemitter for sending messages
// @TODO find a better solution, was only to use it with online event, but not working as expected // @TODO find a better solution, was only to use it with online event, but not working as expected
const eventEmitter = new EventEmitter()
server.decorate('eventEmitter', new EventEmitter())
const xmpp = client({ const xmpp = client({
service: process.env.XMPP_SERVICE, service: process.env.XMPP_SERVICE,
@ -35,7 +36,7 @@ xmpp.on('online', (address) =>
{ {
console.log('connected to ' + address) console.log('connected to ' + address)
eventEmitter.on('send-message', async (data) => server.eventEmitter.on('send-message', async (data) =>
{ {
// Sends a chat message to itself // Sends a chat message to itself
const message = xml( const message = xml(
@ -57,6 +58,7 @@ xmpp.on('offline', (error) => {
xmpp.start().catch(console.error) xmpp.start().catch(console.error)
/** /**
* add routes * add routes
* *
@ -66,8 +68,7 @@ import webhookHttp from './http/api/webhook.js'
server server
.register(webhookHttp, { .register(webhookHttp, {
'prefix': '/api/webhook', 'prefix': '/api/webhook'
'eventEmitter': eventEmitter // @TODO shift to a more fastify-way
}) })
export default server export default server

@ -40,7 +40,7 @@ export default async function(fastify, opts)
const result = parser.run() const result = parser.run()
// send event for send xmpp // send event for send xmpp
opts.eventEmitter.emit('send-message', { fastify.eventEmitter.emit('send-message', {
'message': result 'message': result
}) })

Loading…
Cancel
Save