【问题标题】:AWS API Gateway Websocket UnknownErrorAWS API Gateway Websocket UnknownError
【发布时间】:2019-04-15 11:54:02
【问题描述】:

我们在执行 SDK postToConnection() 作为承诺调用时遇到错误,下面给出了完整的错误详细信息。同一函数中具有不同连接 ID 的其他调用成功发生。预期的 410 连接错误以毫秒为单位正确发生,并且正在得到妥善处理。

然后,此错误需要 40 秒到一分钟多的时间才能返回,这会导致它始终在 Web 套接字 API 中导致“端点请求超时”错误,因为它的最大请求超时为 30 秒。之前有没有人遇到过这个问题和/或实施过任何解决方案?任何解决此问题的想法都将受到高度赞赏,谢谢。

UnknownError:在 Object.extractError (/opt/nodejs/node_modules/aws-sdk/lib/protocol/json.js:51:27) 处与端点通信时出现网络错误

【问题讨论】:

    标签: amazon-web-services websocket aws-api-gateway


    【解决方案1】:

    您是否尝试在连接处理程序中使用 postToConnection ? websocket 连接仅在连接处理程序返回 statusCode 200 之后创建。您不应在连接处理程序内使用 postToConnection。

    【讨论】:

    • 感谢以上建议,现在可以正常使用了!
    • 如何向刚刚连接的客户端发送消息? :)
    • @patrick 看起来客户端应该向单独的路由发送初始请求以接收第一条消息。
    【解决方案2】:

    为避免在无服务器上使用 websocket 时出现 410 问题,请不要忘记捕获您的异常:

    export const ApiGatewayConnector = (event) => {
    
      const endpoint = process.env.IS_OFFLINE
                ? 'http://localhost:3001'
                : `${event.requestContext.domainName}/${event.requestContext.stage}`
                const apiVersion = '2018-11-29'
                return new AWS.ApiGatewayManagementApi({ apiVersion, endpoint })
    }
    

    ....

    if (event.requestContext.stage == 'local') {
                  await ApiGatewayConnector(event)
                  .postToConnection({ ConnectionId, Data })
                  .promise()
                  .catch(_ => removeId(ConnectionId));<----- N.B. Remove disconnected IDs
                } else {
                  await ws.send(Data, ConnectionId)
                }
    }
            
    

    【讨论】:

      【解决方案3】:

      在响应 $connect 事件时调用 .postToConnection 会引发该错误。 您可以在响应 $default 事件时调用 .postConnection 而不会出错。

      // index.js
      // the handler is defined as: index.handler
      
      const AWS = require("aws-sdk");
      
      exports.handler = function (event, context, callback) {
      
          console.log('event.requestContext.eventType', event && event.requestContext && event.requestContext.eventType)
      
          if (event.requestContext.eventType === "CONNECT") {
      
              console.log('$connect event')
      
              // calling apigwManagementApi.postToConnection will throw an exception
      
              callback(null, {
                  statusCode: 200,
                  body: "Connected"
              });
      
          } else if (event.requestContext.eventType === "DISCONNECT") {
      
              console.log('$disconnect event')
      
              // calling apigwManagementApi.postToConnection is pointless since the client has disconneted
      
              callback(null, {
                  statusCode: 200,
                  body: "Disconnected"
              });
          } else {
      
              console.log('$default event')
      
              const ConnectionId = event.requestContext.connectionId
              const bodyString = event.body
              const Data = bodyString
      
              const apigwManagementApi = new AWS.ApiGatewayManagementApi({
                  apiVersion: "2018-11-29",
                  endpoint: event.requestContext.domainName + "/" + event.requestContext.stage
              });
      
              apigwManagementApi
              .postToConnection({ ConnectionId, Data })
              .promise().then(() => {
      
                  callback(null, {
                      statusCode: 200,
                      body: "Disconnected"
                  });
      
              })
      
          }
      
      };
      

      【讨论】:

        猜你喜欢
        • 2021-06-19
        • 2021-04-18
        • 1970-01-01
        • 2021-04-04
        • 2020-10-23
        • 2019-12-19
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        相关资源
        最近更新 更多