AWS API Gateway WebSocket 连接错误

5
我创建了一个API,使用AWS API Gateway和Lambda,与'https://github.com/aws-samples/simple-websockets-chat-app'相同。但是API不能正常工作。当我尝试连接时,出现错误。它的消息是“WebSocket connection to 'wss://b91xftxta9.execute-api.eu-west-1.amazonaws.com/dev' failed: Error during WebSocket handshake: Unexpected response code: 500”。
我的连接代码:

var ws= new WebSocket("wss://b91xftxta9.execute-api.eu-west-1.amazonaws.com/dev");
ws.onopen=function(d){
 console.log(d);
}


响应代码 500 是表示 内部服务器错误 的 HTTP 状态码...因此你的第一步是检查 API Gateway 日志。 - Michael - sqlbot
3个回答

4

尝试将$context.error.validationErrorString$context.integrationErrorMessage添加到阶段日志中。

我在日志格式部分添加了很多内容,例如:

{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp",
"requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod",
"routeKey":"$context.routeKey", "status":"$context.status", 
"protocol":"$context.protocol", "errorMessage":"$context.error.message",
"path":"$context.path", 
"authorizerPrincipalId":"$context.authorizer.principalId",
"user":"$context.identity.user", "caller":"$context.identity.caller", 
"validationErrorString":"$context.error.validationErrorString", 
"errorResponseType":"$context.error.responseType", 
"integrationErrorMessage":"$context.integrationErrorMessage", 
"responseLength":"$context.responseLength" }

在早期开发中,这使我能够看到这种类型的错误:
{
"requestId": "QDu0QiP3oANFPZv=",
"ip": "76.54.32.210",
"requestTime": "21/Jul/2020:21:37:31 +0000",
"httpMethod": "POST",
"routeKey": "$default",
"status": "500",
"protocol": "HTTP/1.1",
"integrationErrorMessage": "The IAM role configured on the integration
    or API Gateway doesn't have permissions to call the integration.
    Check the permissions and try again.",
"responseLength": "35"
}

我添加了这些上下文变量,但是出现了“API_CONFIGURATION_ERROR”错误响应类型,这并没有告诉我更多的信息。 - Jun

2
要获取更多详细信息,请为您的API启用日志记录:阶段 -> 日志/跟踪 -> CloudWatch设置 -> 启用CloudWatch日志。然后,再次发送连接请求并在CloudWatch中监视您的API日志。在我的情况下,我遇到了以下错误:

由于配置错误,执行失败:API Gateway无权承担提供的角色 {arn_of_my_role}

因此,我将API Gateway添加到我的角色的信任关系中,正如此处所述,它解决了问题。

1

请在终端中尝试使用 wscat -c wss://b91xftxta9.execute-api.eu-west-1.amazonaws.com/dev。这应该可以让您连接它。如果您没有安装 wscat,请执行 npm install -g wscat。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接