如何修复socket.io中的net :: ERR_CONNECTION_REFUSED

时间:2019-11-06 15:15:17

标签: html node.js express socket.io api-ai

我正在使用nodejs中的socketIo和api.ai构建一个聊天机器人。 在运行应用程序时,出现以下错误Chrome控制台 GET http://localhost/socket.io/?EIO=3&transport=polling&t=Mv1FwHo net::ERR_CONNECTION_REFUSED

这是我的Index.js

const dotenv=require("dotenv");
dotenv.config();
const APIAI_TOKEN = process.env.APIAI_TOKEN;
const APIAI_SESSION_ID = process.env.APIAI_SESSION_ID;
const express = require('express');
const app = express();
app.use(express.static(__dirname+'/views'));
app.use(express.static(__dirname+'/public'));
const server = app.listen(process.env.PORT ||3000, () => {
    console.log('App listening on port  ', server.address().port, app.settings.env);
});
const io = require('socket.io')(server);
io.on('connection', function(socket){
    console.log('user connected');
})

const apiai = require('apiai')(APIAI_TOKEN);
app.get('/', (req, res) => {
    res.sendFile('index.html');
});
io.on('connection', function(socket){
    socket.on('chat message', (text) => {
        console.log('message: '+ text);
        let apiaiReq = apiai.textRequest(text, {
            sessionID: APIAI_SESSION_ID
        });
        apiaiReq.on('response', (response) => {
            let aiText = response.result.fulfillment.speech;
            console.log('bot reply: '+ aiText);
            socket.emit('bot reply', aiText);
        });
        apiaiReq.on('error', (error) => {
            console.log(error);
        });
        apiaiReq.end();
    });
});

这是我的script.js

const socket = io();
const outputYou = document.querySelector('.yourOut');
const outputBot = document.querySelector('.botOut');

const SpeechRecognition =  window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = 'eu-US';
recognition.interimResults = false;
recognition.maxAlternatives = 2;
document.querySelector('button').addEventListener('click', () => {
    recognition.start();
});
recognition.addEventListener('speechstart', () => {
    console.log('Speech detected');
})
recognition.addEventListener('result', (e) => {
    console.log('result detected');
    let last = e.results.length-1;
    let text = e.results[last][0].transcript;
    outputYou.textContent = text;
    console.log('Confidence: ' + e.results[0][0].confidence);
        socket.emit('chat message', text);

});
recognition.addEventListener('speechend', () => {
    recognition.stop();
});
recognition.addEventListener('error', (e) => {
    outputBot.textContent = "Error: " + e.error;
});
function synthVoice (text){
    const synth = window.speechSynthesis;
    const utterance = new SpeechSynthesisUtterance();
    utterance.text = text;
    synth.speak(utterance);
}
socket.on('bot reply', function(replyText){
    synthVoice(replyText);

    if(replyText == '') replyText = '(Not in mood to answer...)';
    outputBot.textContent = replyText;
})

在nodeJs控制台上,我收到以下错误消息。

` 错误:现在'sessionId'是必需参数。请将此参数添加到“选项 类似于以下示例:

  

var app = ...   request = app.textRequest(“ Hello”,{sessionId:“ UNIQUE_SESSION_ID”})   ...

at TextRequest.QueryRequest (d:\AI-ChatBot\node_modules\apiai\module\query_req
at new TextRequest (d:\AI-ChatBot\node_modules\apiai\module\text_request.js:17
at Application.textRequest (d:\AI-ChatBot\node_modules\apiai\module\apiai.js:1
at Socket.socket.on (d:\AI-ChatBot\index.js:24:30)
at Socket.emit (events.js:198:13)
at d:\AI-ChatBot\node_modules\socket.io\lib\socket.js:528:12
at process._tickCallback (internal/process/next_tick.js:61:11)

`

0 个答案:

没有答案