gpt4 book ai didi

javascript - 在 Express 中使用聊天 irc 机器人发送后无法设置 header

转载 作者:行者123 更新时间:2023-12-03 09:11:53 24 4
gpt4 key购买 nike

我正在构建一个聊天机器人,它将管理聊天并响应用户在聊天中发送的命令。

我还尝试在快速服务器中进行设置,以便我可以启动和停止机器人以及在网络应用程序中管理命令等,这样我就不必从我的终端运行机器人时间。

我的文件设置类似于express-generator,如下

node_modules
routes
- api
- bot.js
api.js
server.js

在 server.js 中

...
var api = require('./routes/api');

app.use('/api/v'+apiVersion, api);
...

routes/api.js

var express = require('express');
var router = express.Router();

// API's
var bot = require('./api/bot');

// Endpoints
router.route('/bot')
.post(function(req,res) { bot.startBot(req,res) })
.delete(function(req,res) { bot.stopBot(req,res) });

module.exports = router;

routes/api/bot.js

var config = require('../../config');

var irc = require('tmi.js');

var chatBotOptions = {
options: {
debug: true,
},
identity: {
username: config.username,
password: config.password
},
channels: config.channels
};
var chatBot = new irc.client(chatBotOptions);

module.exports.startBot = function(req,res){
// Connect chatBot
chatBot.connect();
chatBot.on('join', function(channel, username){
chatBot.say(channel, 'Hello there!');
res.json({action:'joined'});
});
};
module.exports.stopBot = function(req,res){
// Disconnect chatBot
chatBot.say(config.channels[0],"I'm out!");
chatBot.disconnect();
res.json({action:'disconnecting'});
};
chatBot.on('chat', function(channel, user, message, self){
chatBot.say(channel, "Kappa");
});

在我的前端,我使用正确的 http 动词对这些端点进行 ajax 调用来启动和停止机器人。当 startBot 端点被击中,然后再次调用 stopBotstartBot 时,就会出现问题,机器人连接到 irc channel ,但是我想当发送 res.json 消息时我会收到此错误。我正在阅读这与某种回调有关,但我只是不确定那会在哪里,因为 res.json 仅发送到一个地方,我什至不确定这是否是问题出在哪里。

这是错误堆栈

_http_outgoing.js:335
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/Users/jordanriser/workspace/twitch_bot/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/Users/jordanriser/workspace/twitch_bot/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/Users/jordanriser/workspace/twitch_bot/node_modules/express/lib/response.js:249:15)
at client.<anonymous> (/Users/jordanriser/workspace/twitch_bot/routes/api/bot.js:25:9)
at client.emit (events.js:129:20)
at client.handleMessage (/Users/jordanriser/workspace/twitch_bot/node_modules/tmi.js/lib/client.js:362:30)
at /Users/jordanriser/workspace/twitch_bot/node_modules/tmi.js/lib/client.js:602:18
at Array.forEach (native)
at client._onMessage (/Users/jordanriser/workspace/twitch_bot/node_modules/tmi.js/lib/client.js:600:11)
17 Aug 13:39:05 - [nodemon] app crashed - waiting for file changes before starting...

最佳答案

问题与您添加的事件处理程序有关,该事件处理程序在同一个 EventEmitter 实例上执行多次。

所以这个:

chatBot.on('join', function(channel, username){
chatBot.say(channel, 'Hello there!');
res.json({action:'joined'});
});

应该是这样的:

// .once() instead of .on()
chatBot.once('join', function(channel, username){
chatBot.say(channel, 'Hello there!');
res.json({action:'joined'});
});

但是,如果发出请求的客户端在发出 join 事件之前提前关闭连接,这仍然可能会导致错误。为了解决这个问题,您可以在调用 res.json() 之前检查请求的套接字是否仍然可写:

chatBot.once('join', function(channel, username){
chatBot.say(channel, 'Hello there!');
if (req.socket.writable)
res.json({action:'joined'});
});

关于javascript - 在 Express 中使用聊天 irc 机器人发送后无法设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32056574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com