简体   繁体   English

我应该使用什么协议以及它的文档是什么?

[英]What protocol should I use and what are the docs for it?

So I'm trying to build a Discord bot. 所以我正在尝试构建一个Discord机器人。 These types of threads tend to get downvoted a lot on stackoverflow, so I'm hoping this doesn't happen to me. 这些类型的线程倾向于在stackoverflow上得到很多关注,所以我希望这不会发生在我身上。

This particular feature is acting as a temporary solution to my dashboard problem. 此特定功能可作为仪表板问题的临时解决方案。 Due to the nature of glitch.com's hosting, it's supposed to fall asleep after 5 minutes of http inactivity. 由于glitch.com托管的性质,它应该在http不活动5分钟后入睡。 I solved that already by adding a script that pings the URL every 4 minutes, but that caused another issue. 我已经通过添加一个每4分钟ping一次URL的脚本解决了这个问题,但这引起了另一个问题。 I think what's happening is that because that script and the bot script are constantly running, and never technically 'finish', it never lets any incoming connection actually load the webpage. 我认为正在发生的事情是,因为该脚本和僵尸程序脚本一直在运行,并且从不在技术上“完成”,它永远不会让任何传入连接实际加载网页。 So my solution to that problem was to create another glitch project that would act as the dashboard website, and transfer information from the bot project. 因此,我对该问题的解决方案是创建另一个故障项目,该项目将充当仪表板网站,并从bot项目传输信息。 Of course then I'd need to create more scripts that communicate with each other via some internet protocol. 当然,我需要创建更多通过某些互联网协议相互通信的脚本。 The info recorded by the bot is all recorded in a private JSON database using the node-json-db npm library. 机器人记录的信息都使用node-json-db npm库记录在私有JSON数据库中。

My problem is: I don't know what protocol would be best for this kind of thing. 我的问题是:我不知道什么协议最适合这种事情。 Even if I did know, then I'd have to go digging through the docs for the info I'm looking for. 即使我确实知道,那么我必须深入研究文档以获取我正在寻找的信息。

My question is: What protocol should I use, and what docs do I need to read for this? 我的问题是:我应该使用什么协议,以及我需要阅读哪些文档?

I've included some snippets of the code here: 我在这里包含了一些代码片段:

The bot's server code (where I would add the script for communicating with the dashboard): 机器人的服务器代码(我将添加用于与仪表板通信的脚本):

// server.js
// where your node app starts

// init project
const express = require('express');
const app = express();
const JsonDB = require('node-json-db');
const db = new JsonDB("myDataBase", true, true);

// we've started you off with Express, 
// but feel free to use whatever libs or frameworks you'd like through `package.json`.

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/index.html');
});

app.post('/login/c3RvcCBoYWNrZXIh', function(request, response) {
  var servername = request.param('servername');
  var password = request.param('password');
  if (db.getData("/" + servername + "/password") === password) {
response.json(db.getData("/" + servername));
  } else {
response.json(null);
  }
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

// to keep the bot alive, since glitch puts projects to sleep after 5 mins of inactivity.
const http = require('http');
setInterval(() => {
  http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 270000);

The server.js on the dashboard website: 仪表板网站上的server.js:

// server.js
// where your node app starts

// init project
const express = require('express');
const app = express();
const request = require('request');

// we've started you off with Express, 
// but feel free to use whatever libs or frameworks you'd like through `package.json`.

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/index.html');
});



app.post('/login', function(request, response) {
  var servername = request.param('servername');
  var password = request.param('password');
  if ("thereisnopassword" === password) {
    response.sendFile(__dirname + '/dashboard/index.html');
  } else {
    response.sendFile(__dirname + '/views/wronginfo.html');
  }
});

// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

I had this too, but solved it by simply putting the code to start the express server before the http loop. 我也有这个,但是通过简单地将代码放在http循环之前启动express服务器来解决它。

// Require packages
const http = require('http');
const express = require('express');
const app = express();

// Express
app.get("/", (request, response) => {
  response.sendStatus(200);
});
app.listen(process.env.PORT);

// Interval
setInterval(() => {
  http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 240000);

// Bot code
const Discord = require('discord.js');
const client = new Discord.Client();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM