简体   繁体   English

使用Websockets设置AWS ElasticBeanstalk

[英]Setup of AWS ElasticBeanstalk with Websockets

I'm trying to setup Websockets in order to send messages to AWS , so I can then process the message and send some payload to other resources at cloud and deliver custom responses to client part. 我正在尝试设置Websockets以便将消息发送到AWS ,因此我可以处理消息并将一些有效负载发送到云中的其他资源,并将自定义响应传递给客户端。

But, I cannot get that to work. 但是,我无法解决这个问题。

The main target is to send messages to AWS through WSS:// , first approach with WS:// (in case that's possible), depending on payload content, it shall return a custom response. 主要目标是通过WSS:// (使用WS://第一种方法)将消息发送到AWS (在可能的情况下),具体取决于负载内容,它将返回自定义响应。 Then close the connection if no further operation is needed. 如果不需要进一步的操作,则关闭连接。

I've tried the suggestions posted here , here and here . 我已经尝试过这里这里这里发布的建议。 But, either my lack of knowledge about Load Balancing , Websockets , TCP and HTTP is not letting me see pieces of solution missing, I'm doing everything wrong or both. 但是,或者由于我对负载平衡WebsocketsTCPHTTP的了解不足,无法让我看到缺少的解决方案,或者我做错了一切,或者都做错了。

As for now, I have an Elastic Beanstalk example project structure like this: 到目前为止,我有一个Elastic Beanstalk示例项目结构,如下所示:

+ nodejs-v1
|--+ .ebextensions
|  |--- socketupgrade.config
|
|--+ .elasticbeasntalk
|  |--- config.yaml
|
|--- .gitignore
|--- app.js
|--- cron.yaml
|--- index.html
|--- package.json

The Elastic Beanstalk environment and application are standard created, and also made sure that the Balancer is application , not classic , hence the Application Load Balancer can work with Websockets out of the box as many sources and documentation state. Elastic Beanstalk环境和应用程序是标准创建的,并且还确保Balancer是application而不是classic ,因此Application Load Balancer可以与Websockets一起使用许多源和文档状态。

It's setup with HTTP at port 80. Stickiness is enabled for a day. 使用HTTP在端口80上进行设置。粘性启用了一天。

Here's the code being used: 这是使用的代码:

app.js : app.js

'use strict';

const express = require('express');
const socketIO = require('socket.io');
const path = require('path');

const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');

const serber = express()
  .use((req, res) => res.sendFile(INDEX) )
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));

const io = socketIO(serber);

io.on('connection', (socket) => {
  console.log('Client connected');
  socket.on('disconnect', () => console.log('Client disconnected'));
});

setInterval(() => io.emit('time', new Date().toTimeString()), 1000);

index.html : index.html

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
  });
</script>

package.json : package.json

{
  "name": "Elastic-Beanstalk-Sample-App",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express":"*",
    "socket.io":"*"
    },
  "scripts": {
    "start": "node app.js"
  }
}

.ebextensions/socketupgrade.config : .ebextensions/socketupgrade.config

container_commands:
  enable_websockets:
    command: |
     sed -i '/\s*proxy_set_header\s*Connection/c \
              proxy_set_header Upgrade $http_upgrade;\
              proxy_set_header Connection "upgrade";\
      ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf

I'm only getting 504 , 502 , sometimes, when tweaking configurations randomly at pointless tries, it gives me 200 and at other attempts, no protocol error, but messages like disconnection and stuff... 我只得到504502 ,有时候,在毫无意义的尝试随机调整配置时,它给了我200,而在其他的企图,没有协议的错误,但像断线的东西消息...

I appreciate your time and attention reading this desperate topic! 感谢您的时间和精力阅读这个绝望的话题! Any hint will be appreciated as well... Just, anything... TT 任何提示也将不胜感激...随便什么... TT

Thanks for your time and attention! 感谢您的时间和关注!

Kind regards, Jon M. 亲切的问候,乔恩·M。

Update 1 更新1

I'll start quoting @RickBaker: 我将开始引用@RickBaker:

Personally, what I would do first is remove the load balancer from the equation. 就个人而言,我首先要做的是从等式中删除负载均衡器。 >If your ec2 instance has a public ip, go into your security groups and make sure >the proper port your app is listening to is open to the public. >如果您的ec2实例具有公共ip,请进入安全组,并确保>您的应用程序正在侦听的正确端口对公众开放。 And see if you >can at least get it working without the load balancer complicating things. 并查看您是否至少可以在不使负载均衡器使事情复杂化的情况下使其运行。 – >Rick Baker 21 hours ago –> Rick Baker 21小时前

Changed the scaling feature of the Elastic Beanstalk environment's application from Load Balancing, Auto Scaling Environment Type to Single Instance Environment . Elastic Beanstalk环境的应用程序的缩放功能从“ 负载平衡,自动缩放环境类型”更改为“ 单实例环境” Important to know, that I changed it from Elastic Beanstalk web page console, not from EC2 directly, since I think that it can break the Elastic Beanstalk environment application as a whole. 重要的是要知道,我是从Elastic Beanstalk Web控制台更改了它,而不是直接从EC2更改了,因为我认为它可以破坏整个Elastic Beanstalk环境应用程序。

Anyway, changed it, after the environment and environment's application finished setting up again, changed and deployed the following: 无论如何,在环境和环境的应用程序完成设置之后,更改了它,并更改并部署了以下内容:

index.html : index.html

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io();
</script>

After everything got running, tested with a call via webpage to the index page. 一切运行完毕后,通过网页调用索引页面进行测试。 And the logs from node shows life: 来自节点的日志显示寿命:

-------------------------------------
/var/log/nodejs/nodejs.log
-------------------------------------
Listening on 8081
Client connected
Client disconnected
Client connected

Then I started to search for Server to Server setup found this docs and then started to dig up a bit in order to connect to a WSS server. 然后,我开始搜索发现文档的“ 服务器到服务器”安装程序,然后开始进行挖掘以连接到WSS服务器。

So, the main goal is to stablish, and mantain a session from AWS EB application to another server that accepts WSS connections. 因此,主要目标是建立和维护从AWS EB应用程序到另一台接受WSS连接的服务器的会话。 The AWS EB should be responsible of stablish and mantain that connection, so when events happen at Network Server, the application at EB can send responses to the requests of events happening. AWS EB应该负责稳定并维持该连接,因此,当网络服务器上发生事件时, EB上的应用程序可以将响应发送给事件发生。

So then I read this topic , and realized that the NodeJS - socket.io approach won't work based on the posts read. 所以后来我看了这个题目 ,并意识到NodeJS - socket.io的做法是行不通的基础上,帖子读取。 So, I don't know what to do now. 所以,我不知道该怎么办。 ( '-') ('-')

AWS EB can setup environment with Python with WSGI but, geez... Don't know what to do next. AWS EB可以使用带有WSGI Python来设置环境,但是,伙计...不知道下一步该怎么做。 I'll try things in order to connect to WS if possible, if not then WSS , and see if something works out. 如果可能,我将尝试一些事情以便连接到WS ,如果不能,则尝试WSS ,然后看看是否有效果。 So I'll Update right after I have results, whether possitive or not. 因此,无论结果是否正确,我都会在获得结果后立即进行更新。

Jon over and out. 乔恩一遍又一遍。

After combining previous iterations with some more documentation reading, I came to realize that, the connection indeed starts from AWS , via NodeJS using ws . 在将先前的迭代与更多的文档阅读相结合之后,我意识到,连接的确确实是通过AWS通过使用ws NodeJSAWS开始的。

So I'm able to communicate with Network Server via WSS and request and provide data. 因此,我能够通过WSS与Network Server进行通信,并请求并提供数据。

The app.js : app.js

var WebSocket = require('ws');
var wss = new WebSocket('wss://example.com');

wss.on('open', function connection() {
    console.log("WSS connection opening")
});

wss.on('message', function incoming(data) {
    console.log("Jot:")
    console.log(data)
    setTimeout(function timeout() {
        console.log("Sending response")
        wss.send(JSON.stringify(
            {
                "key": "Hi there"
            }
            ));
        }, 
    500);
});

The package.json : package.json

{
  "name": "Elastic-Beanstalk-Sample-App",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express":"*",
    "socket.io":"*",
    "ws": "*"
    },
  "scripts": {
    "start": "node app.js"
  }
}

The structure of the project remains almost the same: 该项目的结构几乎保持不变:

+ nodejs-v1
|--+ .ebextensions
|  |--- socketupgrade.config
|
|--+ .elasticbeasntalk
|  |--- config.yaml
|
|--- .gitignore
|--- app.js
|--- cron.yaml
|--- package.json

As you can see, there's no index.html since it's not used. 如您所见,由于没有使用index.html ,所以没有。

From here, now it's up to the solution requirements the usage of sending/receiving data. 从这里开始,现在取决于解决方案要求的发送/接收数据的用法。 And to make sure the connection is established/recovered. 并确保建立/恢复连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM