使用AWS ELB的连接节点服务器闲置60秒后Websocket关闭 [英] Websocket closing after 60 seconds of being idle while connection node server using AWS ELB

查看:351
本文介绍了使用AWS ELB的连接节点服务器闲置60秒后Websocket关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台节点服务器在EC2实例上运行,并且客户端也在同一EC2实例上运行,客户端打开websocket连接以通信节点服务器,它在QA和Dev AWS环境中运行,但是同一Web连接在60秒后关闭在产品环境中处于空闲状态,我在aws环境中的ELB后面运行客户端和节点服务器。

I have one node server running on EC2 instance and client is also running on same EC2 instance, Client open websocket connection to communicate node server, it is working in QA and Dev AWS environment but same web connection is getting close after 60 seconds of being idle in prod environment ,I am running client and node server behind ELB in aws environment.

客户端代码:

 ws = new WebSocket('ws://localhost:8443');


            ws.onclose = function () {
                console.log("Websocket connection has been closed.");
                clientObj.emit('LogoffSuccess', 'LogoffSuccessfully');
            };
            ws.onerror=function(event)
            {
               console.log(event.data);
            };
            ws.addEventListener('open', function (event) {
                console.log('Websocket connection has been opened');
                ws.send(JSON.stringify(loginCreds));
            });


    Node server Code below:

    const wss = new WebSocket.Server({ server: app });
    const clients = {};
    const idMap = {};

    wss.on(`connection`, ws => {
      const headers = ws.upgradeReq.headers;
      const host = headers.host;
      const key = ws.upgradeReq.headers[`sec-websocket-key`];

      ctiServer.on(`responseMessage`, message => {
        clients[message.AgentId].send(JSON.stringify(message));
      });

      ws.on(`message`, message => {
        log.info(`Message received. Host: ${host}, Msg: ${message}`);
        if (JSON.parse(message).EventName === `Login`) {
          clients[JSON.parse(message).AgentId] = ws;
          idMap[key] = JSON.parse(message).AgentId;
        }
        ctiServer.processIncomingRequest(message);
      });

      ws.on(`close`, () => {
        log.info(`Connection closed. Host: ${host}`);
        const message = {
          EventName: `Logoff`,
          AgentId: idMap[key],
          EventData: {}
        };

      });
    });


推荐答案


默认情况下,弹性负载平衡将空闲超时值设置为60秒。因此,如果目标在请求进行过程中至少每60秒没有发送一些数据,则负载均衡器可以关闭前端连接。为了确保文件上传等冗长的操作有时间完成,请在每个空闲超时时间过去之前发送至少1个字节的数据,并根据需要增加空闲超时时间的长度。

By default, Elastic Load Balancing sets the idle timeout value to 60 seconds. Therefore, if the target doesn't send some data at least every 60 seconds while the request is in flight, the load balancer can close the front-end connection. To ensure that lengthy operations such as file uploads have time to complete, send at least 1 byte of data before each idle timeout period elapses, and increase the length of the idle timeout period as needed.

>: //docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#connection-idle-timeout

请注意,定期发送流量以保持连接状态可以最好地满足您的兴趣。您可以在应用程序负载平衡器中将空闲超时设置为最多4000秒,但是您会发现有状态的中间网络基础结构(防火墙,NAT设备)往往会在连接实际上空闲很长时间之前重置连接。

Note that your interests are best served by periodically sending traffic to keep the connection alive. You can set the idle timeout to up to 4000 seconds in an Application Load Balancer, but you will find that stateful intermediate network infrastructure (firewalls, NAT devices) tends to reset connections before they are actually idle for so long.

这篇关于使用AWS ELB的连接节点服务器闲置60秒后Websocket关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆