【问题标题】:Angular & Electron - IPC Communication and Service method callAngular 和 Electron - IPC 通信和服务方法调用
【发布时间】:2018-05-13 19:05:14
【问题描述】:

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

由于主进程和渲染进程之间的通信是异步的,服务方法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`;
     ....
    }
}

【问题讨论】:

    标签: angular electron


    【解决方案1】:

    setTimeout(() => {

       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);
    

    【讨论】:

    • 我已经发布了在setinterval上有触发器的代码。下面添加了主要流程
    【解决方案2】:
    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']
          });
      }
    
       }) 
    

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 2019-07-14
      • 2021-11-09
      • 2019-02-13
      • 2016-11-21
      • 2021-12-04
      • 1970-01-01
      • 2018-10-13
      • 2021-01-15
      相关资源
      最近更新 更多