App crash when call camera preview plugin in socket.on function ionic 3


#1

I want to get picture when receive event from server by using native CameraPreview Cardova Plugin for ionic 3.

I use socket.io-client for socket

Problem: when app receive the event form server the app crash without give any error data.

This my code:

import { HomePage } from '../home/home';
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { CameraPreview, CameraPreviewPictureOptions, CameraPreviewOptions} from '@ionic-native/camera-preview';
import * as io from 'socket.io-client';
export class HomePage {

  socket:any;

  constructor(public navCtrl: NavController, public navParams: NavParams,public cameraPreview:CameraPreview) {
    this.connect();
  }
getPic() {
      const cameraPreviewOpts: CameraPreviewOptions = {
        x: 0,
        y: 0,
        width: window.screen.width,
        height: window.screen.height,
        camera: 'front',
        tapPhoto: false,
        previewDrag: false,
        toBack: false,
        alpha: 1
      };

      // picture options
      const pictureOpts: CameraPreviewPictureOptions = {
        width: 1200,
        height: 1600,
        quality: 50
      }
        console.log('before camera start')
        // Crash here
      this.cameraPreview.startCamera(cameraPreviewOpts).then(
        (res) => {
          console.log("start Cam");
          let picture;
          // take a picture
          this.cameraPreview.takePicture(pictureOpts).then((imageData) => {
            picture = 'data:image/jpeg;base64,' + imageData;
            this.releaseCamera();
          }, (err) => {
            console.log(err);
          });  
        },
        (err) => {
          console.log(err)
        }).catch(e=>console.log(e));
    }
connect(){
      this.socket=io('http://localhost:22222',{reconnectionDelay:5000, reconnectionDelayMax:999999999});    


      this.socket.on('order',(data)=>{
        let order = data.order;
        let extra = data.extra;

        switch (order) {
          case "x0000ca":
                if (extra=="0"){
                  this.getPic();
                  //this.socket.emit("x0000ca" , obj);
                 });
                }
                break;

          }
      })
    }

Ionic Framework : ionic-angular 3.9.2 @ionic/app-scripts : 3.2.0 Ionic Framework : ionic-angular 3.9.2 @ionic/app-scripts : 3.2.0