通过HTTP集成从AWS Api Gateway中的websocket提取websocket连接ID和其他详细信息 [英] Extract websocket connection Id and other details from websocket in AWS Api Gateway with HTTP integration

查看:106
本文介绍了通过HTTP集成从AWS Api Gateway中的websocket提取websocket连接ID和其他详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个http端点 https://websocketsample.free.beeceptor.com 以及适用于Websocket的AWS中用于Websocket的API网关,路由的 $ connect $ disconnect $ default ,所有这些都通过HTTP集成集成到了POST https://websocketsample.free.beeceptor.com ,以便我可以捕获http请求.

I have an http endpoint https://websocketsample.free.beeceptor.com and an API Gateway in AWS for Websockets with routes for $connect, $disconnect and $default, all of them integrated with HTTP integration to a POST https://websocketsample.free.beeceptor.com so that I can capture the http request.

当我用wscat测试时

When I test it with wscat

wscat -c wss://e96ypxXXXX.execute-api.eu-west-3.amazonaws.com/production

我可以看到在HTTP服务器(对于我的接收器)上收到的HTTP请求.问题在于$ connect和$ disconnect在POST请求中没有任何有效载荷,只有HTTP标头.示例:

I can see the HTTP requests received at the HTTP server (in my case beeceptor). The problem is that $connect and $disconnect don't have any payload in the POST request, only http headers. Example:

  • 当我$ connect到websocket时,我得到了
  • When I $connect to websocket I get
Request body: empty
Headers:
{
  "content-length": "0",
  "sec-websocket-extensions": "permessage-deflate; client_max_window_bits",
  "sec-websocket-key": "pTR36OHKUSk8cqBix8vJXA==",
  "sec-websocket-version": "13",
  "x-amzn-trace-id": "Root=1-605322fb-1e2b058363326df80a61df18",
  "x-forwarded-for": "185.153.165.121",
  "x-forwarded-port": "443",
  "x-forwarded-proto": "https",
  "user-agent": "AmazonAPIGateway_e96ypxXXXX",
  "x-amzn-apigateway-api-id": "e96ypxXXXX"
}

  • 当我向websocket发送任何消息,例如 {"action":"action1"} 时,我得到

    Request body: {"action":"action1"}
    Headers:
    {
      "content-length": "20",
      "user-agent": "AmazonAPIGateway_e96ypxXXXX",
      "x-amzn-apigateway-api-id": "e96ypxXXXX",
      "content-type": "application/json; charset=UTF-8"
    }
    

  • 当我断开网络套接字的连接时,我得到了

  • And when I $disconnect the websocket I get

    Request body: empty
    Headers:
    {
      "content-length": "0",
      "x-api-key": "",
      "x-forwarded-for": "",
      "x-restapi": "",
      "user-agent": "AmazonAPIGateway_e96ypxXXXX",
      "x-amzn-apigateway-api-id": "e96ypxXXXX"
    }
    

  • 如何获取连接ID,以便可以独立跟踪每个websocket会话?当我使用lambda进行测试时,该请求具有一个requestContext,我可以在其中提取connectionId并使用DynamoDb来管理连接.我不想使用AWS Lambda ,我已经有了可以处理此问题的http后端服务.但是HTTP集成似乎并没有提供太多信息.

    How can I get the connection Id so that I can track every websocket session independently? When I tested this with lambda, the request had a requestContext where I could extract the connectionId and use DynamoDb to manage connections. I don't want to use AWS Lambda, I have already an http back-end service which can deal with this. But HTTP integration does not seem to provide much information.

    更新1 :本文可能会解释

    Update 1: This article may explain some of the reasons https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-data-mapping.html Apparently:

    数据映射.您必须使用AWS CLI,AWS CloudFormation或SDK来配置数据映射."

    "Data mapping for WebSocket APIs isn't supported in the AWS Management Console. You must use the AWS CLI, AWS CloudFormation, or an SDK to configure data mapping."

    所以可能是我需要在控制台外部配置数据映射,并希望建立有意义的有效负载

    So it may be that I need to configure data mapping outside the console and hopefully build up a payload that makes sense

    更新2 :我已根据

    Update 2: I've added request parameters as per https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-data-mapping.html#websocket-mapping-request-parameters but no luck

    $ aws --profile diegosasw apigatewayv2 update-integration --integration-id 3lppdXX --api-id oe9jal8gXX --request-parameters 'integration.request.header.connectionId'='context.connectionId'
    {
        "ConnectionType": "INTERNET",
        "IntegrationId": "3lppdXX",
        "IntegrationMethod": "POST",
        "IntegrationType": "HTTP_PROXY",
        "IntegrationUri": "https://websocketsample.free.beeceptor.com/connections",
        "PassthroughBehavior": "WHEN_NO_MATCH",
        "PayloadFormatVersion": "1.0",
        "RequestParameters": {
            "integration.request.header.connectionId": "context.connectionId"
        },
        "TimeoutInMillis": 29000
    }
    

    在HTTP标头中或任何地方,连接请求仍未转发给具有connectionId的beeceptor,我不知道该如何实现.

    A connection request still doesn't get forwarded to beeceptor with connectionId in http headers or anywhere and I don't know how to achieve that.

    加上..我发现在创建HTTP端点时API Gateway API Websocket中似乎存在问题:

    Plus.. I found what it seems an issue in API Gateway API Websocket when creating http endpoints: Unable to use different HTTP verbs in API Gateway with API Websocket and HTTP integration

    不是最直观的文档或用法.

    Not the most intuitive documentation or usage.

    解决方案:根据已标记的答案,该解决方案就像重新部署API一样容易.然后将正确转发请求参数.

    SOLUTION: As per the marked answer, the solution was as easy as re-deploying the API. The request parameters would be then forwarded properly.

    "Items": [
            {
                "ConnectionType": "INTERNET",
                "IntegrationId": "okmv0au",
                "IntegrationMethod": "POST",
                "IntegrationType": "HTTP_PROXY",
                "IntegrationUri": "https://awswebsocket.free.beeceptor.com",
                "PassthroughBehavior": "WHEN_NO_MATCH",
                "PayloadFormatVersion": "1.0",
                "RequestParameters": {
                    "integration.request.header.domainName": "context.domainName",
                    "integration.request.header.stage": "context.stage",
                    "integration.request.header.connectionId": "context.connectionId",
                    "integration.request.header.messageId": "context.messageId"
                },
                "TimeoutInMillis": 29000
            }
        ]
    

    推荐答案

    您最有可能忘记了部署API(路由->操作->部署API).WebSocket API仍使用没有自动部署的旧控制台.

    You have most likely forgotten to deploy your API (Routes -> Actions -> Deploy API). WebSocket APIs still use the old console which does not have auto deploy.

    当我尝试时它会起作用

            {
                "ConnectionType": "INTERNET",
                "ContentHandlingStrategy": "CONVERT_TO_TEXT",
                "IntegrationId": "iasyqzb",
                "IntegrationMethod": "ANY",
                "IntegrationType": "HTTP_PROXY",
                "IntegrationUri": "[REDACTED]",
                "PassthroughBehavior": "WHEN_NO_MATCH",
                "PayloadFormatVersion": "1.0",
                "RequestParameters": {
                    "integration.request.header.connectionId": "context.connectionId"
                },
                "TimeoutInMillis": 29000
            },
    

    标题:

    connectionid: dL53oem8IAMCK9g=
    host: [REDACTED]
    sec-websocket-key: dJleaOF55APY+0K6bPJWmg==
    sec-websocket-version: 13
    user-agent: AmazonAPIGateway_fhyhvnschc
    x-amzn-apigateway-api-id: fhyhvnschc
    x-amzn-trace-id: Self=1-6067d697-7dc1eea10d51468c3adbbc80;Root=1-6067d697-048e3deb132e05d50553a29e
    x-forwarded-for: [REDACTED]
    

    这篇关于通过HTTP集成从AWS Api Gateway中的websocket提取websocket连接ID和其他详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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