【发布时间】:2020-04-28 03:40:25
【问题描述】:
我正在编写一个云函数来启动或停止我正在使用的游戏面板的服务器。除非请求完成,否则一切正常,它永远不会触发导致 Firebase 挂起的“数据”、“结束”或“关闭”事件。有没有其他人遇到过这个?我用谷歌搜索“收到 204 响应后节点 http 挂起”,但没有找到任何相关信息。
import * as functions from "firebase-functions";
import * as http from "https";
import * as qs from "querystring";
export const commandServer = functions.https.onRequest((request, response) => {
if (request.body.command !== "start" && request.body.command !== "stop") {
response.status(400).send("Error, missing information.");
}
console.log(request.body.origin);
const apiKey = "<apiKey>";
const panelServerId = "<panelId>";
const data = qs.stringify({
signal: request.body.command === "start" ? "start" : "stop",
});
const options = {
protocol: "https:",
hostname: "<server url>",
port: 443,
path: `/api/client/servers/${panelServerId}/power?`,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": data.length,
Authorization: `Bearer ${apiKey}`,
},
};
const req = http.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", () => {
response.status(200).send(`Successfuly sent ${data} command`);
});
});
req.on("error", (error) => {
console.log("error: ", error);
response.status(500).send("Oops, something went wrong!");
});
req.write(data);
req.end();
});
【问题讨论】:
-
我希望你正在制定计划,因为 Firebase 不允许第三方从免费帐户中的功能调用。
-
@Sandeep 我已经这样做了,我必须早点弄清楚。
-
好的。可以尝试使用 fetch / axios 。我过去曾尝试过
标签: javascript node.js firebase google-cloud-functions