Angular&电子 - IPC通信和服务方法调用

时间:2017-11-29 23:22:39

标签: angular electron

我正在使用Angular + Electron App并尝试通过订阅主进程中的事件广播来设置服务中的apiBaseUrl

由于main和render进程之间的通信是异步的,因此服务方法getAll()很早就被调用,因此在apiBaseUrl上未定义。

在所有接线之前我是否可以进行IPC通信,并在使用之前设置apiBaseUrl

以下是我的服务代码

import { ElectronService } from 'ngx-electron';
import { IpcRenderer } from 'electron';

export class SiteService {

    private apiBaseUrl:string;
    private ipc: IpcRenderer;

    constructor(private http: Http,
        private electronService: ElectronService) {

        if (this.electronService.isElectronApp) {

            this.ipc = this.electronService.ipcRenderer;

            this.ipc.send('request-apiBaseUrl');

            this.ipc.on('apiBaseUrl-changed', (evt, url) => {

                this.apiBaseUrl= url;
            });
        }
    }

    getAll(): Observable<IEditSite[]> {

        let url = `${this.apiBaseUrl}site/all`;
     ....
    }
}

2 个答案:

答案 0 :(得分:0)

setTimeout(()=&gt; {

   document.getElementById("nextSlide").click();
  }, 10000);

  //setTimeout(this.carousel(), 2000);
  console.log(this.deviceInfo);
  this.getDesktopNotiDetails();
  this.getDesktopNotiDetails();
  this.notificationCount();
  this.getBriefItemCount();

  //    code to trigger notification
  this.setintervalID = setInterval(() => {
   //  document.getElementById("nextSlide").click();
   this.getDesktopNotiDetails();
   this.notificationCount();
   this.getBriefItemCount();
   let text: string;
   text = String(this.notificationnumber);
   var canvas = document.createElement('canvas');
   canvas.height = 140;
   canvas.width = 140;
   var ctx = canvas.getContext('2d');
   ctx.fillStyle = 'red';
   ctx.beginPath();
   ctx.ellipse(70, 70, 70, 70, 0, 0, 2 * Math.PI);
   ctx.fill();
   ctx.textAlign = 'center';
   ctx.fillStyle = 'white';

   if (text.length > 2) {
    ctx.font = 'bold 65px "Segoe UI", sans-serif';
    ctx.fillText('' + text, 70, 95);
   } else if (text.length > 1) {
    ctx.font = 'bold 85px "Segoe UI", sans-serif';
    ctx.fillText('' + text, 70, 100);
   } else {
    ctx.font = 'bold 100px "Segoe UI", sans-serif';
    ctx.fillText('' + text, 70, 105);
   }

   var badgeDataURL = canvas.toDataURL();
   var img = this._electronService.remote.nativeImage.createFromDataURL(badgeDataURL);

   var mainWindow = this._electronService.remote.getCurrentWindow();
   mainWindow.setOverlayIcon(img, "sushant");
   //
   this.deviceInfo = this.deviceService.getDeviceInfo();
   if (this.deviceInfo.os === "mac") {

    this._electronService.ipcRenderer.send('displayIconMsg', this.notificationnumber);
   }

  }, 10000);

答案 1 :(得分:0)

ipcMain.on('sync', (event, arg) => {
  if(os.platform()==="darwin"){
    var iconpath =path.join(__dirname, 'app_icon_256px.png.icns')
  }
    else{
    var iconpath =path.join(__dirname, 'app_icon_512px.ico')
  }
    // Print 3
    var notificationDetails =JSON.parse(arg)
    console.log(notificationDetails);
    let  url =notificationDetails.meta_value;
    / . $imgurlArray[2];
    notifier.notify({
      title: notificationDetails.MESSAGE,
      message: notificationDetails.SUMMARY,
      icon:iconpath  , // Absolute path (doesn't work on balloons)
      silent:true, // Only Notification Center or Windows Toasters
      wait: true // Wait with callback, until user action is taken against notification
    });

    notifier.on('click', function(notifierObject, options) {
      console.log("It worked");

       shell.openExternal(notificationDetails.meta_value);


    });
    // Send value synchronously back to renderer process

});


ipc.on('displayIconMsg' ,(event, arg) => {

       console.log(arg);
       if(parseInt(arg)!=0){
       app.setBadgeCount(parseInt(arg));
       }

       if(parseInt(arg)===0){
       app.setBadgeCount(parseInt(arg));
       }


 })


ipc.on('displayDialog' ,(event, arg) => {

  if(os.platform()==="darwin"){
     var iconpath =path.join('app_icon_256p.icns');

    dialog.showMessageBox({
      type: 'info',
      message:arg,


      buttons: ['Ok']
    });
  }
    else{
      console.log("sushant");
      dialog.showMessageBox({
        type: 'info',
        message:arg,

      icon :nativeImage.createFromPath('app_icon_512px.ico'),
        buttons: ['Ok']
      });
  }

   })