node.jstypescriptxmppnode-xmpp

Create Multiple Jabber Client Echo Bot


Hai Stackoverflowers, i had try so many ways to fix my problem and i end up with stack over flow

Programming Language: Typescript

my main goals: create Client Class as many as i want and can turn on and turn off the Client, the client will be as Bot that echo text from the friendlist

I use node-xmpp-client Library : https://github.com/chris-rock/node-xmpp-client

My Code :

import * as Client from 'node-xmpp-client'; // Jabber Helper

export class TerminalJabber {
  private ter;
  private client;

  constructor(ter: any) {
    this.ter = ter;
    this.client = new Client({
      jid: this.ter.settings.username,
      password: this.ter.settings.password,
    });
  }

  public turnOn(){
    // Turn on Jabber
    this.client.on('online', () => {
      console.log('online');
      const stanza = new Client.Element('presence', { })
         .c('show').t('chat').up()
         .c('status').t('Happily echoing your <message/> stanzas');
      this.client.send(stanza);
    });

    this.client.on('stanza', (stanza) => {
      // Message come here and will echo from here
    });

     // Catch error
     this.client.on('error', (e) => {
       console.log('------>', e);
     });
  }
  // Turn Off Jabber
  public turnOff () {
    // Turn Off Jabber
    this.client.end();
    return true;
  }
}

The Problem is, The Bot Can Start, but never will be disconnect when i call a new class and turnOn() and turnOff().

How I call My code: // Hit URL /turnOn/:terId

// Hit URL
app.router.get(/turnOn/:id, turnOnController); // for turn On
app.router.get(/turnOff/:id, turnOfController); // for turn Off

TurnOnController(){
  // Get data from database using id
  const data = Query ~ ;
  const terminal = new TerminalId(data)
  terminal.turnOn();
}

TurnOffController(){
// Get data from database using id
  const data = Query ~ ;
  const terminal = new TerminalID(data)
  terminal.turnOff();
}

Maybe you have a solution of my problems ? have been thinking and read so many post about xmpp, but i cant solve this things,

Please help

sincerely, Thank you


Solution

  • You need to call the turnOff method on the terminal instance before you create new one. This also means you need to have access to the instance from both controllers.

    var terminals = {};
    
    function turnOnController(){
    
        var id = getIdSomehow();
    
        terminal[id] = new TerminalId(data);
        terminal[id].turnOn();
    
    }
    
    function turnOffController(){
    
        var id = getIdSomehow();
    
        if (terminal[id])
            terminal[id].turnOff();
    
    }