我继承了一个web应用程序,它的后端是用Go Lang编写的,使用gRPC和协议缓冲区与Angular 8前端通信。老开发人员使用Wails将Go方法绑定到前端,并根据需要构建一个exe。
根据客户的需求,我们决定停止使用Wails,转而使用Electron (14.0.0)。
我想知道是否有一种方法可以将gRPC与电子一起使用。我找到的所有教程都与旧版本的Electron有关。
谢谢!
发布于 2021-09-20 09:42:29
我以前用过Wails V2,虽然它做得很好。但它有点有限。我换成了Electron。
我也有类似的设置。Go中的后端服务器和Svelte中的前端。我使用超文本传输协议服务器而不是gRPC。但是这个过程是一样的。
所以,当你在电子上捆绑你的应用时。您需要将后端的二进制文件添加到其他资源中。
然后,从前端启动并连接到grpc后端服务器。使用https://github.com/grpc/grpc-node。像这样
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是这样的。
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
https://stackoverflow.com/questions/69251237
复制相似问题