小编Dha*_*ram的帖子

使用AWS ELB在连接节点服务器闲置60秒后Websocket关闭

我有一个节点服务器在EC2实例上运行,客户端也在相同的EC2实例上运行,客户端打开websocket连接来与节点服务器通信,它在QA和Dev AWS环境中工作,但是相同的Web连接在空闲60秒后关闭在prod环境中,我在aws环境中运行ELB后面的客户端和节点服务器.

客户代码:

 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 => …
Run Code Online (Sandbox Code Playgroud)

amazon-ec2 amazon-web-services websocket node.js aws-elb

5
推荐指数
1
解决办法
3011
查看次数

AWS ALB 从 lambda 返回 502 Bad Gateway

我有一个 lambda 函数,它返回 base64 字符串,当我从它的代码中调用 lambda 时,它可以工作,但是当我在 ALB 后面调用 lambda 并且 base64 字符串很大时,ALB 给我错误 502 Bad Gateway。注意:对于小字符串 ALB 也有效。

// Lambda function handler

'use strict';


module.exports.handler = async (event, context) => {
  // ALB expects following response format
  // see: https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html
  const response = {
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
    },
    isBase64Encoded: true,
    statusCode: 200,
    statusDescription: '200 OK',
  };
// ALB gives error 502 when buffer size is large
  const answer = 'This is my audio buffer'.toString('base64');
  response.body = …
Run Code Online (Sandbox Code Playgroud)

node.js aws-lambda aws-sdk-nodejs nodejs-stream aws-alb

5
推荐指数
1
解决办法
5899
查看次数