Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71531

Chrome.sockets.tcp problem to receive onReceive.addListener

$
0
0

@andreamori wrote:

Hello!
I’m using the cordova plugin chrome.sockets.tcp. I have created a provider for managing sockets operations in my Ionic 3 app.

Whenever I use the “invioPacchetto” (english -> sendPackage) function of this provider, socket creation and connection is successful … and the package is sent with a reply by device to which I am connected.
The onReceive.addListener event (present in the “invioPacchetto” function of this provider) sometimes receives the answer and sometimes not! I’m going crazy.

I’m 100% sure that the deviceto which I am connected responds to the send received from the app but the onReceive.addListener event does NOT occur on the app

N.B. I debug the device I communicate with in real time, so I see everything that arrives and leaves via sockets connections… and everything works

Here is the provider code with which I manage all sockets communications.

This is the code of provider (sorry for comments inside):

import { Platform, LoadingController } from 'ionic-angular';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';

@Injectable()
export class TCPServices {

  public array_qui:Array<Object> = [];
  public tipo_platform: string;
  public array_risposta_invioPacchetto:Array<Object> = [];
  public appoggio_tempo: number;

  constructor(public http: HttpClient, public loadingCtrl: LoadingController, platform:Platform) {
    console.log('Hello FunzioniSocketsProvider Provider');

    platform.ready().then((readySource) => {
      //inizio misurazione del tempo
      this.appoggio_tempo = Date.now();

      if(readySource=='cordova'){
        this.tipo_platform = readySource;

        this.array_qui['piattaforma'] = this.tipo_platform;
        this.array_qui['tempo_esecuzione'] = (Date.now()-this.appoggio_tempo);
      
      } else {
        this.tipo_platform = "browser";

        this.array_qui['piattaforma'] = "browser";
        this.array_qui['tempo_esecuzione'] = (Date.now()-this.appoggio_tempo);
      }

      console.log("constructor FunzioniSocketsProvider -> "+this.array_qui['piattaforma']);
    });
  }

  testfunzione(){
    console.log("controllo platform su TCPServices -> "+this.tipo_platform);
    //alert("controllo platform su TCPServices -> "+this.tipo_platform);

    return new Promise(resolve => {
      resolve(this.array_qui);

    });
  }

  arrayBuffer2str(buf) {
		var str= '';
		var ui8= new Uint8Array(buf);
		for (var i= 0 ; i < ui8.length ; i++) {
			str= str+String.fromCharCode(ui8[i]);
		}
		return str;
	}

	str2arrayBuffer(str) {
		var buf= new ArrayBuffer(str.length);
		var bufView= new Uint8Array(buf);
		for (var i= 0 ; i < str.length ; i++) {
			bufView[i]= str.charCodeAt(i);
  		}
		return buf;
  }

	invioPacchetto(ipAddr,ipPort,testo_loader,data,tipo_funzione_per_test) {
    return new Promise(resolve => {
      if(this.tipo_platform=='cordova'){
        if(data!=''){
          //se ci sono dati da inviare -> proseguo l'operazione
          console.log("siamo in invioPacchetto ;-) ... e i dati da inviare ci sono");
          
          //carica componente loading
          let loading = this.loadingCtrl.create({
            content: testo_loader,
            duration: 5000
          });
          loading.present();

          var delay= 5000;	/// 5 seconds timeout
          (<any>window).chrome.sockets.tcp.create({}, createInfo => { //callback function with createInfo as the parameter
            var _socketTcpId= createInfo.socketId;
            console.log("invioPacchetto A1 ... idsockets: "+_socketTcpId);

            (<any>window).chrome.sockets.tcp.connect(_socketTcpId, ipAddr, ipPort, result => { //callback function with result as the parameter
              if (result === 0) {
                this.array_risposta_invioPacchetto['result'] = result;
                this.array_risposta_invioPacchetto['testo_errore'] = '';

                var data2send= this.str2arrayBuffer(data);
                /// connection ok, send the packet
                (<any>window).chrome.sockets.tcp.send(_socketTcpId, data2send);
              } else {
                //error of connection
                this.array_risposta_invioPacchetto['result'] = result;
                this.array_risposta_invioPacchetto['testo_errore'] = 'Errore di connessione rilevato ...';
                this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '';

                //restituisci errore di connessione
                resolve(this.array_risposta_invioPacchetto);
                //togli loading
                loading.dismiss();
              }
            });
            (<any>window).chrome.sockets.tcp.onReceive.addListener( info => { //callback function with info as the parameter
              /// recived, then close connection
              //(<any>window).chrome.sockets.tcp.close(_socketTcpId); chiude l'attuale processo -> mai io devo chiudere il socket della risposta!
              (<any>window).chrome.sockets.tcp.close(info.socketId);
              this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = this.arrayBuffer2str(info.data);

              resolve(this.array_risposta_invioPacchetto);
              //togli loading
              loading.dismiss();
            });
            (<any>window).chrome.sockets.tcp.onReceiveError.addListener( info => {
              (<any>window).chrome.sockets.tcp.close(info.socketId);
              this.array_risposta_invioPacchetto['result'] = -999; //codice errore scelto da me
              this.array_risposta_invioPacchetto['testo_errore'] = this.arrayBuffer2str(info.data);
              this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = ''; //essendo un errore
              
              resolve(this.array_risposta_invioPacchetto);
              //togli loading
              loading.dismiss();
            });
            /// set the timeout
            setTimeout(function() {
              (<any>window).chrome.sockets.tcp.close(_socketTcpId);
              this.array_risposta_invioPacchetto['testo_errore'] = 'Timeout connessione socket eseguita';
              this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '';

              resolve(this.array_risposta_invioPacchetto);
              //togli loading
              loading.dismiss();
            }, delay);
          });

        } else {
          //nessuna dato passato da inviare
          this.array_risposta_invioPacchetto['result'] = -1;
          this.array_risposta_invioPacchetto['testo_errore'] = 'Nessuna stringa passata da inviare tramite sockets ...';
          this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '';

          resolve(this.array_risposta_invioPacchetto);
        }
      } else {
        let loading = this.loadingCtrl.create({
          content: testo_loader,
          duration: 1000
        });
        loading.present();

        //dati di risposta con la stessa struttura del socket sopra in cordova
        this.array_risposta_invioPacchetto['result'] = -1;
        this.array_risposta_invioPacchetto['testo_errore'] = 'La connessione Socket non è attiva perchè siamo su '+this.tipo_platform;
        
        if(tipo_funzione_per_test=='lista funzioni'){
          //risposta vuota -> come dovrebbe essere ;-)
          //this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '';
        
          // *** LISTA FUNZIONI *** risposta con dati -> per fare i test ;-)
          this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '{"Info":{"HW":"ICON100 demo","ID":"101.100.100.100","NAME":"ICON500","VERSION":"0.4.42.32","TYPE":1,"TIMER":"1 minuto 5 secondi","cmd_supp":["Funzione demo ;-)","Info","Program","GetRTConfig","GetRTVal"]}}';
        }

        if(tipo_funzione_per_test=='invio programmazione'){
          // *** INVIO PROGRAMMAZIONE *** simulazione di valore "Error" restituito
          //this.array_risposta_invioPacchetto['testo_errore'] = 'Similuazione di errore...';
          //this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '{"Program":"impianto.exp","Chunk":1,"Total":4,"Error":"Invalid CRC"}';
          // *** INVIO PROGRAMMAZIONE *** simulazione pacchetto ricevuto OK
          //this.array_risposta_invioPacchetto['testo_errore'] = '';
          //this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '{"Program":"impianto.exp","Chunk":1,"Total":3,"TimeMS":4210}';

          /*** SIMULAZIONE ERRORE GENERALE SOCKETS PER INVIO PROGRAMMAZIONE
          this.array_risposta_invioPacchetto['result'] = -999; //codice errore scelto da me
          this.array_risposta_invioPacchetto['testo_errore'] = 'Un testo di errore a caso...';
          this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = ''; //essendo un errore
          */
        }

        /*** LISTE PER REALTIME */
        if(tipo_funzione_per_test=='liste realtime'){
          this.array_risposta_invioPacchetto['testo_errore'] = '';
          this.array_risposta_invioPacchetto['data_risposta_in_stringa'] = '{"GetRTConfig":{"Chunk":1,"Total":1,"Len":1208,"CRC":"b1e7","BASE64":"eyJvYmpzIjpbeyJpZCI6IjAwMC4wMDAuMDA1LjAyMV8wMDAiLCJkZXNjIjoiSW5jcmVtLiBDdXRvZmYgQW1iLiIsIm1heCI6IjEwMC4wIiwibWluIjoiMC4wIiwidW0iOiImZGVnO0MiLCJiZyI6IiNDMEMwQzAiLCJmb3IiOiIjMDAwMDAwIiwidmlzIjoiVEVTVE8yIiwiUk8iOiIwIiwibW9kIjoiRk9STV9GTE9BVF8wMSIsInBhZyI6IlBhcmFtZXRyaSIsIlgiOiI0NTBweCIsIlkiOiIyMHB4IiwiRFgiOiIyMDBweCIsIkRZIjoiNjRweCJ9LAp7ImlkIjoiMDAwLjAwMC4wMDUuMDI1XzAwMSIsImRlc2MiOiJDdXRvZmYgRXh0IiwibWF4IjoiMTAwLjAiLCJtaW4iOiIwLjAiLCJ1bSI6IiZkZWc7QyIsImJnIjoiI0MwQzBDMCIsImZvciI6IiMwMDAwMDAiLCJ2aXMiOiJURVNUTzIiLCJSTyI6IjAiLCJtb2QiOiJGT1JNX0ZMT0FUXzAxIiwicGFnIjoiU2lub3R0aWNvIiwiWCI6IjY3NXB4IiwiWSI6IjIwcHgiLCJEWCI6IjIwMHB4IiwiRFkiOiI2NHB4In0sCnsiaWQiOiIwMDAuMDAwLjAwNS4wMTVfMDAyIiwiZGVzYyI6Ik1pbiBULiBNYW5kLiIsIm1heCI6IjEwMC4wIiwibWluIjoiMC4wIiwidW0iOiImZGVnO0MiLCJiZyI6IiNDMEMwQzAiLCJmb3IiOiIjMDAwMDAwIiwidmlzIjoiVEVTVE8yIiwiUk8iOiIwIiwibW9kIjoiRk9STV9GTE9BVF8wMSIsInBhZyI6IlBhcmFtZXRyaSIsIlgiOiIwcHgiLCJZIjoiMTU5cHgiLCJEWCI6IjIwMHB4IiwiRFkiOiI2NHB4In1dCn0="}}';
        }

        //togli loading
        //loading.dismiss();

        resolve(this.array_risposta_invioPacchetto);
      }
    });
	}

}

Any ideas ?
I’m looking for help! Give me a hand, please

p.s. this could also become a job for a freelance

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 71531

Trending Articles