Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何使用gRPC #520

Open
Zcentury opened this issue Dec 17, 2024 · 0 comments
Open

如何使用gRPC #520

Zcentury opened this issue Dec 17, 2024 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@Zcentury
Copy link

我想使用grpc来调用其他服务,遇到了困惑我很久的问题

import { ipcMain } from "electron";
import { fileURLToPath } from "url";
import { dirname } from "path";
import path from "node:path";

const __dirname = dirname(fileURLToPath(import.meta.url));

var PROTO_PATH = path.resolve(__dirname, "helloworld.proto");

console.log(PROTO_PATH);

import grpc from "@grpc/grpc-js";
import protoLoader from "@grpc/proto-loader";

var packageDefinition = protoLoader.loadSync(PROTO_PATH, {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true,
});
var hello_proto = grpc.loadPackageDefinition(packageDefinition)
    .helloworld as any;
var client = new hello_proto.Greeter(
    "localhost:50051",
    grpc.credentials.createInsecure()
);

export default (win: Electron.BrowserWindow) => {
    ipcMain.on("hello", () => {
        client.SayHello({ name: "user" }, function (err: any, response: any) {
            console.log("Greeting:", response.message);
        });
    });
};

根据grpc官网的node例子写的

但是一直报错

PROTO_PATH: E:\electron-vite-project\example.proto
App threw an error during load
TypeError: Cannot read properties of null (reading 'readFileSync')
    at fetch2 (file:///E:/electron-vite-project/dist-electron/main.js:7673:29)
    at Root2.load2 [as load] (file:///E:/electron-vite-project/dist-electron/main.js:7702:9)
    at Root2.loadSync2 [as loadSync] (file:///E:/electron-vite-project/dist-electron/main.js:7712:17)
    at loadProtosWithOptionsSync (file:///E:/electron-vite-project/dist-electron/main.js:11043:28)
    at Object.loadSync2 [as loadSync] (file:///E:/electron-vite-project/dist-electron/main.js:11762:61)
    at file:///E:/electron-vite-project/dist-electron/main.js:20577:33
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadApplicationPackage (file:///E:/electron-vite-project/node_modules/electron/dist/resources/default_app.asar/main.js:129:9)
    at async file:///E:/electron-vite-project/node_modules/electron/dist/resources/default_app.asar/main.js:241:9

我甚至写的绝对路径都不行

还有就是如何将这个文件也打包到dist-electron中去

@Zcentury Zcentury added the help wanted Extra attention is needed label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant