Добрый день из Испании, я пытаюсь установить промежуточный сервер nodejs между клиентом и настоящим ботом, но когда я пытаюсь установить сокет-соединение с моим ботом, он говорит, что произошел сбой протокола рукопожатия.
Прямо сейчас мой код
Сервер app.js
require('dotenv').config();
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require("express-session");
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const Sequelize = require('sequelize');
const sequelize = new Sequelize(process.env.DBNAME, process.env.DBUSER, process.env.DBPASSWORD, {
dialect: 'mysql'
});
const sequelizeStoreSession = new SequelizeStore({ db: sequelize })
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// default value for title local
app.locals.title = 'Human Data Demo';
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// SESSION PERSISTANCE USING COOKIES
sequelizeStoreSession.sync();
// Enable authentication using session + passport
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
cookie: {
httpOnly: true,
maxAge: 2419200000
},
store: sequelizeStoreSession
}))
require('./passport/index')(app);
const index = require('./routes/index');
app.use('/', index);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
module.exports = app;
Файл маршрута, который я использую для подключения сервера к боту
router.post('/messages/', (req, res, next) => {
const client = new WebSocketClient();
console.log('cliente: ', client);
client.on('connectFailed', err => {
console.log(err);
next(err);
})
client.on('connect', connection => {
console.log('connected');
connection.on('error', function (error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function () {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function (message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
}
});
connection.send(req.body.text);
})
client.connect('http://55dce78c.ngrok.io/api/messages', 'echo-protocol')
console.log('connectado ', client)
})
Ифактический бот
var restify = require('restify');
var builder = require('botbuilder');
var http = require('http');
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('listening to %s', server.name, server.url);
});
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
const bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
bot.dialog('/', function (session, args) {
session.send('Hello World');
});
Не могли бы вы дать мой совет?
Редактировать: Это ошибка, которую я получаю прямо сейчас
POST /api/project/messages - - ms - - Error: Server responded with a non-101 status: 502 Bad Gateway Response Headers Follow: cache-control: no-cache connection: close content-type: text/html at WebSocketClient.failHandshake (C:\Users\David\Desktop\HumanData\Proyecto Acceso API\API_FINAL\node_modules\websocket\lib\WebSocketClient.js:326:32) at ClientRequest.<anonymous> (C:\Users\David\Desktop\HumanData\Proyecto Acceso API\API_FINAL\node_modules\websocket\lib\WebSocketClient.js:265:18) at emitOne (events.js:116:13) at ClientRequest.emit (events.js:211:7) at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:543:21) at HTTPParser.parserOnHeadersComplete (_http_common.js:112:17) at Socket.socketOnData (_http_client.js:440:20) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at addChunk (_stream_readable.js:263:12) at readableAddChunk (_stream_readable.js:250:11) at Socket.Readable.push (_stream_readable.js:208:10) at TCP.onread (net.js:597:20)