Beispiel #1
0
  setupClient() {

    //10.56.240.217

    let client = net.createConnection(4222, "10.56.241.15", () => {
      this.updateChatter('opened client on ' + JSON.stringify(client.address()));
    });

    client.on('data', (data) => {

      var buffer = new Buffer(data, 'utf8')
      this.updateChatter('Client Received: ' + buffer.toString());
      if (buffer.toString() === 'PING') {
        this.client.write('PONG' + lf)
      }

    });

    client.on('error', (error) => {
      this.updateChatter('client error ' + error);
    });

    client.on('close', () => {
      this.updateChatter('client close');
    });

    this.client = client

  }
Beispiel #2
0
NetCore.createConnection = function(options, cb) {
    client = net.createConnection(options, ()=> {
        console.log("--------create connection succeed!");

        if (cb != null) {
            cb();
        }

        client.on('error', function(error) {
            console.log(">>>>>>>>>error:" + error);
        });

        client.on('data', function(data) {
            NetCore._processMsg(data);
        });
    });
}
  function init () {
    //
    self.jid = conf.login + '@' + conf.domain + '/' + conf.resource;
    conn = tcp.createConnection(conf.port || 5222, conf.host);
    self.brocker_ack_enabled = conf.brocker_ack_enabled || false
    self.end_to_end_ack_enabled = conf.end_to_end_ack_enabled || false

    if(self.brocker_ack_enabled) {
      self.brocker_ack_queue = conf.brocker_ack_queue;
    }

    if(self.end_to_end_ack_enabled) {
      var config = {
        'retry_interval_sec':15,
        'queue_name':"end_to_end_ack_queue"
      }
      self.end_to_end_ack_queue = new AsyncStorageBackedQueue.AsyncStorageBackedQueue(config);
    }

    conn.on("disconnect", function (error) {
      // self.emit('close');
    });

    conn.on("connect", function () {
      this.setTimeout(0);
      this.setEncoding("utf8");
      initiateSession();
    });

    conn.on("data", function (data) {
      console.log('[ recv ] ' + data);
      this.last_time_received = new Date;
      var l = "<?xml version='1.0'?>".length;
      if(typeof data !== 'string') {
        data = data.toString();
      }
      if(typeof data === 'string') {
        if (data.substring(0, l) == "<?xml version='1.0'?>") {
          data = data.substring(l);
        }
        parser.parseString(data);
      }
    });

    conn.on("error", function (error) {
      //console.error(error);
      var timeout = Math.round(Math.random() * 10 + 5) * 1000;
      //setTimeout(init, timeout);
      console.log(timeout);
      self.emit('message.error',error);
    });

    conn.on("close", function (error) {
      self.emit('close',error);
    });

    conn.on("end", function () {
      try{
        this.write('</stream:stream>', function(e) {
          console.log(e);
        });
      }catch(e){
        return {
          "error":e
        };
      }
      // self.emit('close');
    });
  }