首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将gRPC与Electron 14配合使用

将gRPC与Electron 14配合使用
EN

Stack Overflow用户
提问于 2021-09-20 08:24:49
回答 1查看 400关注 0票数 1

我继承了一个web应用程序,它的后端是用Go Lang编写的,使用gRPC和协议缓冲区与Angular 8前端通信。老开发人员使用Wails将Go方法绑定到前端,并根据需要构建一个exe。

根据客户的需求,我们决定停止使用Wails,转而使用Electron (14.0.0)。

我想知道是否有一种方法可以将gRPC与电子一起使用。我找到的所有教程都与旧版本的Electron有关。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-09-20 09:42:29

我以前用过Wails V2,虽然它做得很好。但它有点有限。我换成了Electron。

我也有类似的设置。Go中的后端服务器和Svelte中的前端。我使用超文本传输协议服务器而不是gRPC。但是这个过程是一样的。

所以,当你在电子上捆绑你的应用时。您需要将后端的二进制文件添加到其他资源中。

然后,从前端启动并连接到grpc后端服务器。使用https://github.com/grpc/grpc-node。像这样

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

在我的例子中,我使用电子生成器(https://www.electron.build/)来构建我的应用程序。

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

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

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

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

我的应用程序是针对MacOS的,electron-builder.yaml是这样的。

代码语言:javascript
复制
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

电子生成器配置示例:https://github.com/twiny/svelte-electron-tailwind/blob/main/electron-builder.yml

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

https://stackoverflow.com/questions/69251237

复制
相关文章

相似问题

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