【问题标题】:What protocol should I use and what are the docs for it?我应该使用什么协议以及它的文档是什么?
【发布时间】:2018-10-22 03:50:45
【问题描述】:

所以我正在尝试构建一个 Discord 机器人。这些类型的线程往往在 stackoverflow 上被否决,所以我希望这不会发生在我身上。

此特定功能可作为我的仪表板问题的临时解决方案。由于glitch.com's 托管的性质,它应该在http 不活动5 分钟后进入睡眠状态。我已经通过添加一个每 4 分钟 ping 一次 URL 的脚本解决了这个问题,但这又导致了另一个问题。我认为正在发生的事情是因为该脚本和机器人脚本一直在运行,并且从技术上讲永远不会“完成”,它永远不会让任何传入连接实际加载网页。所以我对这个问题的解决方案是创建另一个故障项目,作为仪表板网站,并从机器人项目传输信息。当然,我需要创建更多通过某种互联网协议相互通信的脚本。 bot 记录的信息全部记录在使用 node-json-db npm 库的私有 JSON 数据库中。

我的问题是:我不知道哪种协议最适合这种事情。即使我确实知道,我也必须翻阅文档以获取我正在寻找的信息。

我的问题是:我应该使用什么协议,为此我需要阅读哪些文档?

我在这里包含了一些代码的 sn-ps:

机器人的服务器代码(我将在其中添加与仪表板通信的脚本):

// 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);

仪表板网站上的 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);
});

【问题讨论】:

    标签: javascript node.js protocols discord.js


    【解决方案1】:

    我也有这个问题,但是通过简单地将启动快速服务器的代码放在 http 循环之前来解决它。

    // 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();
    

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 2011-08-20
      • 1970-01-01
      • 2018-09-17
      • 2014-11-02
      • 2019-10-06
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多