我有一个运行Websocket的节点快递服务器,在客户端,一个在容器内部工作的ts连接,但是我尝试从外部进行连接,但无法到达。
这是来自服务器的代码:
`
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const http = require('http');
const socketio = require('socket.io');
const app = express();
const profile = process.env.profile ? process.env.profile : 'devaws';
const config = require(`${__dirname}${path.sep}config${
path.sep
}${profile.trim()}`);
const status = 'INIT';
app.use(
'/health',
require('express-healthcheck')({
healthy: function() {
return { status: status };
}
})
);
app.use(
'/info',
require('express-healthcheck')({
healthy: function() {
return {
build: {
artifact: config.name,
name: config.name,
group: config.group
}
};
}
})
);
app.use(
express.static(path.join(__dirname, 'dist'), {
index: 'index.html'
})
);
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/dist', '/index.html'), {
enconding: 'utf8'
});
});
/* ************** Server ************** */
let server = http.createServer(app);
const io = socketio(server); //iniciando el server de socket.io
server.listen(config.port, () => {
port = server.address().port;
ip = server.address();
console.log(server);
console.log('------------------------------------------------');
console.log(`Profile: ${profile}`);
console.log(`Application Address: ${config.host}:${config.port}`);
console.log('------------------------------------------------');
console.log(`Socket server running in : ${ip}:${config.port}`);
console.log(ip);
});
/* ************** Server ************** */
io.on('connection', function(socket) {
//imprimiendo el id del cliente conectado
console.log(`client: ${socket.id}`);
});`
同一台服务器和容器中的客户端连接io.connect()不会出现问题,但是当我尝试使用http://dns:4000/socket.io从外部连接时,出现以下错误:
与'wss://simulator.cashlight.io:4000 / socket.io /?EIO = 3&transport = websocket'的WebSocket连接失败:在建立连接之前关闭WebSocket。
然后这个:
与'wss://simulator.cashlight.io:4000 / socket.io /?EIO = 3&transport = websocket'的WebSocket连接失败:连接建立错误:net :: ERR_CONNECTION_TIMED_OUT
我需要从其他容器从外部到达此套接字服务器。像这样伸出手
有帮助吗?