积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《结合 gRPC 使用 Electron 14》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
我继承了一个 Web 应用程序,其后端是用 Go Lang 编写的,并使用 gRPC 和协议缓冲区与 Angular 8 前端进行通信。老开发人员正在使用Wails将Go方法绑定到前端并根据需要构建一个exe。
根据客户需求,我们决定停止使用 Wails 并切换到 Electron (14.0.0)。
我想知道是否有一种方法可以将 gRPC 与 Electron 结合使用。我找到的所有教程 涉及旧版本的 Electron。
正确答案
我以前使用过wails v2,它的表现非常好。但它有点有限。我切换到 electron。
我有类似的设置。 go 中的后端服务器和 svelte 中的前端。我使用 http 服务器而不是 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
理论要掌握,实操不能落!以上关于《结合 gRPC 使用 Electron 14》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
