API 网关 WebSocket:同一 Lambda 函数中的不同处理程序
API Gateway WebSocket: different handlers in the same Lambda function
我正在尝试创建一个使用 API 网关 WebSocket 连接到 AWS Lambda 函数的基本示例。
我遵循了 this link 的例子。
目标是拥有一个具有多个入口点(函数)的 Lambda csproj,与上例中指定的相同。
API 网关:
我有四个路由,它们都连接到同一个 lambda 函数:cgavan-websocket-2
:
$connect
$disconnect
echo
$default
Lambda 函数:
我有一个具有四种不同功能的 lambda 项目:
Connect.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
Disconnect.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
Echo.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
Default.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
问题:
如何为每个 API 网关路由指定从 Lambda csproj 调用哪个函数处理程序?
现在,当我连接到 API 网关 WebSocket(使用 WebSocket Request Route: $connect
)时,它总是调用 Default.FunctionHandler()
.
这与 websocket-api 的实现类似,它在被触发时发送随机消息。这里event_typeMESSAGE
是根据您的需要定制的
import time
import json
import boto3
def lambda_handler(event, context):
print(event)
event_type = event["requestContext"]["eventType"]
if event_type == "CONNECT" or event_type == "DISCONNECT":
response = {'statusCode': 200}
return response
elif event_type == "MESSAGE":
connection_id = event["requestContext"]["connectionId"]
domain_name = event["requestContext"]["domainName"]
stage = event["requestContext"]["stage"]
message = f'{domain_name}: {connection_id}'.encode('utf-8')
api_client = boto3.client('apigatewaymanagementapi', endpoint_url = f"https://{domain_name}/{stage}")
for _ in range(5):
api_client.post_to_connection(Data=message,
ConnectionId=connection_id)
time.sleep(5)
response = {'statusCode': 200}
return response
我正在尝试创建一个使用 API 网关 WebSocket 连接到 AWS Lambda 函数的基本示例。
我遵循了 this link 的例子。
目标是拥有一个具有多个入口点(函数)的 Lambda csproj,与上例中指定的相同。
API 网关:
我有四个路由,它们都连接到同一个 lambda 函数:cgavan-websocket-2
:
$connect
$disconnect
echo
$default
Lambda 函数:
我有一个具有四种不同功能的 lambda 项目:
Connect.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
Disconnect.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
Echo.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
Default.FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
问题:
如何为每个 API 网关路由指定从 Lambda csproj 调用哪个函数处理程序?
现在,当我连接到 API 网关 WebSocket(使用 WebSocket Request Route: $connect
)时,它总是调用 Default.FunctionHandler()
.
这与 websocket-api 的实现类似,它在被触发时发送随机消息。这里event_typeMESSAGE
是根据您的需要定制的
import time
import json
import boto3
def lambda_handler(event, context):
print(event)
event_type = event["requestContext"]["eventType"]
if event_type == "CONNECT" or event_type == "DISCONNECT":
response = {'statusCode': 200}
return response
elif event_type == "MESSAGE":
connection_id = event["requestContext"]["connectionId"]
domain_name = event["requestContext"]["domainName"]
stage = event["requestContext"]["stage"]
message = f'{domain_name}: {connection_id}'.encode('utf-8')
api_client = boto3.client('apigatewaymanagementapi', endpoint_url = f"https://{domain_name}/{stage}")
for _ in range(5):
api_client.post_to_connection(Data=message,
ConnectionId=connection_id)
time.sleep(5)
response = {'statusCode': 200}
return response