我不确定我这样做是否正确。我的最终目标是将来自lambda函数的发布请求发送到我的 Electron 应用程序,并创建系统通知。在本地,我可以从 postman 那里完成此操作,但是当我在Linux上安装该应用程序时,它不起作用,现在我不确定应该将请求指向何处,在开发中我也指出了它。 http://localhost:3000/通知您在安装应用程序时会发生什么。我将如何向应用程序发送发布请求,最终我想建立用户帐户,因此我将需要基于lambda逻辑将请求发送给每个单独的用户。
我在用 Electron express ,还有另一种处理邮寄要求的方法。
到目前为止,这是我的main.js文件中的代码
"use strict";
const { app, BrowserWindow } = require("electron");
const { Notification } = require("electron");
const express = require("express");
const bodyParser = require("body-parser");
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
win.loadFile("index.html");
win.webContents.openDevTools();
}
app.whenReady().then(createWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Create a new instance of express
const appE = express();
// Tell express to use the body-parser middleware and to not parse extended bodies
appE.use(bodyParser.json());
// Route that receives a POST request to /sms
appE.post("/notify", function (req, res) {
const body = req.body;
console.log(body);
res.set("Content-Type", "text/plain");
function showNotification() {
const notification = {
title: "Basic Notification",
body: `You sent: ${body.message} to Express`,
};
new Notification(notification).show();
}
app.whenReady().then(createWindow).then(showNotification);
res.send(`You sent: ${body.message} to Express`);
});
// Tell our app to listen on port 3000
appE.listen(3000, function (err) {
if (err) {
throw err;
}
console.log("Server started on port 3000");
});
最佳答案
您的方法是错误的。通常,用户本地网络之外运行的任何内容都无法访问在Electron应用程序中运行的HTTP服务器,因此您的Lambda函数将无法连接到该服务器。
如果要从Lambda函数读取通知,则需要对其进行轮询(在计时器上发出Ajax请求)。
如果要将消息从服务器推送到应用程序,则仍然需要从客户端启动连接(以便可以在用户的本地网络外部进行连接)。典型的方法是使用Websockets。尽管它们不是Lambda函数友好的,但是由于它们需要始终处于打开状态,因此您将需要托管服务器来监听其他系统(例如EC2)上的Websocket连接。如果您的基础架构已经为Lambda功能做好了准备,那么您可以编写一个基础架构,当该基础架构被触发时,它会向EC2服务器发送一条消息以触发Websocket消息。
+-------+ +-------+ +-------+
| | HTTP | | | |
| λ | Request |HTTP | Websocket |Client |
| ----------------Server ------------------ |
+-------+ +-------+ +-------+
关于javascript - 快速应用程序位于 Electron 应用程序内部的网址是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64683190/