首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular & Electron - IPC通信和服务方法调用

Angular & Electron - IPC通信和服务方法调用
EN

Stack Overflow用户
提问于 2017-11-30 07:22:39
回答 2查看 759关注 0票数 2

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

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

有没有什么地方可以在连接所有东西之前进行IPC通信,并在使用之前设置apiBaseUrl

以下是我的服务代码

代码语言:javascript
复制
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`;
     ....
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-04-14 21:36:03

setTimeout(() => {

代码语言:javascript
复制
   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);
票数 0
EN

Stack Overflow用户

发布于 2018-04-14 21:41:00

代码语言:javascript
复制
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']
      });
  }

   }) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47563511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档