简体   繁体   中英

Event listeners don't seem to be triggering

I am working with the Asterisk ARI Node.js client and would like to listen for certain events and then perform an action. From my understanding after connecting to the server you can setup several different types of event listeners for events being published via WebSockets to perform tasks. In my code below I don't receive any output even though I am triggering these specific events and can connect via WSCat and watch the events streaming.

The app I am building should just listen for the events to occur and update a database. I will never need to access the Node application through an HTTP request which is why I returning forbidden on every request to the server. My end goal is to just have this application sitting on a server reacting to events.

'use strict';
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const client = require('ari-client');
const util = require('util');

const server = http.createServer((req, res) => {
    res.statusCode = 403;
    res.end('FORBIDDEN');
});

server.listen(port, hostname, () => {
    client.connect('http://127.0.0.1:8088', 'username', 'password')
        .then(function(ari) {
            ari.on('DeviceStateChanged', function(event) {
                console.log(event);
            })
            ari.on('ChannelCreated', function(event) {
                console.log(event);
            })
            ari.on('BridgeCreated', function(event) {
                console.log(event);
            })
            ari.on('StasisStart', function(event) {
                console.log(event);
            })
            ari.on('PeerStatusChange', function(event) {
                console.log('blah', event);
            })
            ari.on('Dial', function(event) {
                console.log('Dial', event);
            })
    })
    .catch(function(err) {
        console.log(err);
    })
});

Why create a server then? You might test the following.

'use strict';
const client = require('ari-client');
const util = require('util');

client.connect('http://127.0.0.1:8088', 'username', 'password')
        .then(function(ari) {
            ari.on('DeviceStateChanged', function(event) {
                console.log(event);
            })
            ari.on('ChannelCreated', function(event) {
                console.log(event);
            })
            ari.on('BridgeCreated', function(event) {
                console.log(event);
            })
            ari.on('StasisStart', function(event) {
                console.log(event);
            })
            ari.on('PeerStatusChange', function(event) {
                console.log('blah', event);
            })
            ari.on('Dial', function(event) {
                console.log('Dial', event);
            })
    })
    .catch(function(err) {
        console.log(err);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM