【问题标题】:Serverless WebSockets - No method found matching route @connections/* for http method POST无服务器 WebSockets - 找不到与 http 方法 POST 的路由 @connections/* 匹配的方法
【发布时间】:2021-01-13 07:39:31
【问题描述】:

我正在使用无服务器框架来托管我的 WebSocket,它具有更新我的连接数据库的典型 $connect、$disconnect、$default 等方法:

case '$connect':
  await dynamoDb.put({
    TableName: process.env.CONNECTIONS_TABLE,
    Item: {
      connectionId,
      // Expire the connection an hour later. This is optional, but recommended.
      // You will have to decide how often to time out and/or refresh the ttl.
      ttl: parseInt((Date.now() / 1000) + 3600)
    }
  }).promise();

我的 WebSocket 设置是:

WebSocket URL: wss://1111111111.execute-api.ap-southeast-2.amazonaws.com/dev/
Connection URL: https://1111111111.execute-api.ap-southeast-2.amazonaws.com/dev/@connections

我的 HTTP 设置是:

Invoke API at: https://222222222.execute-api.ap-southeast-2.amazonaws.com/dev/

我有一个广播功能,我用它来向连接发送数据,我正在调用它:

sls invoke --function broadcast --data '{ \"body\": \"Hello from server\" }'

源向每个连接发送一条消息,在请求的参数中提供:

async function sendMessage(connectionId, body) {
  try {
    await apig.postToConnection({
      ConnectionId: connectionId,
      Data: body
    }).promise();
  } catch (err) {
    // Ignore if connection no longer exists
    if(err.statusCode !== 400 && err.statusCode !== 410) {
      throw err;
    }
  }
}

async function getAllConnections(ExclusiveStartKey) {
  const { Items, LastEvaluatedKey } = await dynamoDb.scan({
    TableName: process.env.CONNECTIONS_TABLE,
    AttributesToGet: [ 'connectionId' ],
    ExclusiveStartKey
  }).promise();

  const connections = Items.map(({ connectionId }) => connectionId);
  if(LastEvaluatedKey) {
    connections.push(...await getAllConnections(LastEvaluatedKey));
  }
  return connections;
}

module.exports.handler = async function(event, context) {
  const { body } = event;
  const connections = await getAllConnections();

  await Promise.all(
    connections.map(connectionId => sendMessage(connectionId, body))
  );
}

可以建立连接(我可以连接到 WebSocket),但是当我尝试调用此函数时,我收到错误:

没有找到与 http 方法 POST 的路由 @connections/ZE4SDfSJSwMCJ4g%3D 匹配的方法。

ZE4SDfSJSwMCJ4g 是我的 connectionId,它存在于我的数据库中。我不确定这个路由问题是否与我的 HTTP API 和我的 WebSocket API 指向不同的 API Gateway URL 有关?

感谢您的帮助!

【问题讨论】:

    标签: javascript websocket serverless


    【解决方案1】:

    确保您的 API Gateway 管理配置中的端点与您的 ws 端点相同。

    const agma = new AWS.ApiGatewayManagementApi({
      apiVersion: AGMA_VERSION,
      endpoint: WS_ENDPOINT // 1111111111.execute-api.ap-southeast-2.amazonaws.com/dev
    })
    

    【讨论】:

    • 这个!我盲目地将生成端点的代码从属于我的 WS API 的 lambda 复制并粘贴到属于我的 REST API 的新 lambda
    【解决方案2】:

    我也遇到了 websockets 和谷歌云功能的问题。我认为这是因为 Web 套接字服务器已进入睡眠状态并且没有主动侦听,因为它是无服务器的。

    函数被配置为在 http 请求(端口 80/443)上唤醒并重新进入睡眠状态,因此需要专门配置它们以唤醒 web socket 端口上的请求。

    这似乎可以使用无服务器 websockets 插件https://github.com/serverless/serverless-websockets-pluginhttps://www.serverless.com/blog/api-gateway-websockets-example 有一篇文章方法

    【讨论】:

    • 该插件已弃用,它现在存在于当前版本的无服务器框架中。我认为这不是问题。
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多