使用 Electron 14 与 gRPC

huangapple go评论86阅读模式
英文:

Using gRPC with Electron 14

问题

我继承了一个使用Go语言编写的Web应用程序,它使用gRPC和协议缓冲区与Angular 8前端进行通信。旧的开发者使用Wails将Go方法绑定到前端,并构建一个exe文件。

根据客户需求,我们决定停止使用Wails,并切换到Electron(14.0.0)。

我想知道是否有一种方法可以在Electron中使用gRPC。我找到的所有教程都是关于旧版本的Electron。

英文:

I've inherited a web application, whose backend is written in Go Lang and uses gRPC and protocol buffers to communicate with the Angular 8 frontend. The old developer was using Wails to bind Go method to the frontend and to build an exe, as required.

Based on the client needs, we decided to stop using Wails and to switch to Electron (14.0.0).

I was wondering if there is a method to use gRPC with Electron. All the tutorials I've found
concerns older versions of Electron.

答案1

得分: 4

我之前使用过Wails V2,它的表现非常好,但有一些限制。我转而使用Electron。

我有一个类似的设置,后端使用Go语言,前端使用Svelte。我使用的是HTTP服务器而不是gRPC,但过程是相同的。

因此,在将应用程序打包到Electron中时,您需要将后端的二进制文件添加到附加资源中。

然后,从前端开始并连接到gRPC后端服务器,可以使用https://github.com/grpc/grpc-node,像这样:

const spawn = require('child_process').execFile;

let binaryPath = process.resourcesPath + "/app.asar.unpacked/bin/macos/myapp";

// 启动服务器
function startBackend() {
	child = spawn(binaryPath, ['serve', "-p", port, "-s", userDataPath]);
}

在我的情况下,我使用了electron-builder(https://www.electron.build/)来构建我的应用程序。

您可以使用extraResources指令将后端二进制文件添加到Electron应用程序中。

https://www.electron.build/configuration/contents.html#extraresources

或者

binaries: ["bin/windows/app.exe"]

asarUnpack: ["bin/windows/app.exe"]

我的应用程序是为MacOS开发的,electron-builder.yaml文件如下所示:

productName: MyApp
appId: com.electron.${name}
remoteBuild: false

compression: normal
npmRebuild: true

asar:
  smartUnpack: true

directories:
  output: out
  buildResources: build

mac:
  category: public.app-category.developer-tools
  icon: ./icons/512x512.png
  darkModeSupport: false
  target:
    - target: dmg
    # - target: zip


  fileAssociations:
    - ext: svg
      role: Viewer
  # extraResources:
  #   - from: "bin/macos/myapp"
  #     to: "bin/macos/myapp"
  #     filter:
  #       - "**/*"

  binaries: ["bin/macos/myapp"]
  asarUnpack: ["bin/macos/myapp"]
    
  hardenedRuntime: false
  gatekeeperAssess: false
  entitlements: "build/macos/entitlements.mac.plist"
  entitlementsInherit: "build/macos/entitlements.mac.plist"
  publish: null
  
dmg:
  sign: true

electron builder配置示例:https://github.com/twiny/svelte-electron-tailwind/blob/main/electron-builder.yml

英文:

I previously used Wails V2, while its does very good job. but its kinda limited. I switched to Electron.

I had a similar setup. a Backend server in Go and frontend in Svelte. I used HTTP server instead of gRPC. bud the process is the same.

So, when bundling your app in electron. you will need to add the binary of the backend to additional resources.

And then, from the frontend you start and connect to the grpc backend server. using https://github.com/grpc/grpc-node. like this

const spawn = require('child_process').execFile;

let binaryPath = process.resourcesPath + "/app.asar.unpacked/bin/macos/myapp"

// run server
function startBackend() {
	child = spawn(binaryPath, ['serve', "-p", port, "-s", userDataPath]);
}


In my case i used electron builder (https://www.electron.build/) to build my app.

you can use extraResources directive to add your backend binary to electron app.

https://www.electron.build/configuration/contents.html#extraresources

or

binaries: ["bin/windows/app.exe"]

asarUnpack: ["bin/windows/app.exe"]

my app was for MacOS and electron-builder.yaml looks like this.

productName: MyApp
appId: com.electron.${name}
remoteBuild: false

compression: normal
npmRebuild: true

asar:
  smartUnpack: true

directories:
  output: out
  buildResources: build

mac:
  category: public.app-category.developer-tools
  icon: ./icons/512x512.png
  darkModeSupport: false
  target:
    - target: dmg
    # - target: zip


  fileAssociations:
    - ext: svg
      role: Viewer
  # extraResources:
  #   - from: "bin/macos/myapp"
  #     to: "bin/macos/myapp"
  #     filter:
  #       - "**/*"

  binaries: ["bin/macos/myapp"]
  asarUnpack: ["bin/macos/myapp"]
    
  hardenedRuntime: false
  gatekeeperAssess: false
  entitlements: "build/macos/entitlements.mac.plist"
  entitlementsInherit: "build/macos/entitlements.mac.plist"
  publish: null
  
dmg:
  sign: true

Example of electron builder config:https://github.com/twiny/svelte-electron-tailwind/blob/main/electron-builder.yml

huangapple
  • 本文由 发表于 2021年9月20日 16:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69251237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定