我正在设置aws elasticbeanstalk服务器,并希望支持TCP连接。
我完成的步骤:
const socketHandler = require('./util/handler');
const logger = require('./util/Logger')
const { createServer } = require('http');
const express = require('express');
const WebSocket = require('ws');
const app = express();
const server = createServer(app);
const ws = new WebSocket.Server({ server });
ws.on('connection', (socket,req) => {
socket.send('Hello')
console.log('CLIENT CONNECTED');
logger.info({title: "User Connected", message : `IP ${req.connection.remoteAddress}`, line : "50", file:"util/handler.js"})
socket.on('message', (data) => socketHandler.dataResolver(socket,data));
socket.on('error', (err) => socketHandler.errorHandler(err));
socket.on('close', () => socketHandler.leaveHub(socket))
});
ws.on('error' , (err) => {
console.log(err)
})
server.listen(3050, function() {
console.log('Listening on http://localhost:3050');
});
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:3050;
keepalive 256;
}
server {
listen 8080;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
access_log /var/log/nginx/access.log main;
location / {
proxy_pass http://nodejs;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location /static {
alias /var/app/current/static;
}
}
container_commands:
removeconfig:
command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"
const WebSocket = require('ws');
const ws = new WebSocket('ws://elasticbeanstalk_url');
ws.onopen = function () {
console.log('CONNECTED')
};
ws.onmessage = function({data}) {
console.log(data)
}
ws.onerror = function (err) {
console.log(err.error)
}
我遇到错误:套接字挂起
Error: socket hang up
at connResetException (internal/errors.js:559:14)
at Socket.socketOnEnd (_http_client.js:433:23)
at Socket.emit (events.js:208:15)
at endReadableNT (_stream_readable.js:1168:12)
at processTicksAndRejections (internal/process/task_queues.js:77:11) {
code: 'ECONNRESET'
}
我正在寻找将近15个小时的解决方案。