我通过AWS API Gateway和Lambda创建了一个与“ https://github.com/aws-samples/simple-websockets-chat-app”相同的API。但是API无法正常工作。尝试连接时出现错误。其消息是“与'wss://b91xftxta9.execute-api.eu-west-1.amazonaws.com/dev'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:500”
我的连接代码
var ws= new WebSocket("wss://b91xftxta9.execute-api.eu-west-1.amazonaws.com/dev");
ws.onopen=function(d){
console.log(d);
}
答案 0 :(得分:1)
尝试将 $ 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"
}
答案 1 :(得分:0)
尝试在终端中使用wscat -c wss://b91xftxta9.execute-api.eu-west-1.amazonaws.com/dev。这应该允许您连接它。如果您没有安装wscat,只需执行npm install -g wscat
答案 2 :(得分:0)
要获取更多详细信息,请为您的API启用日志记录:Stages -> Logs/Tracing -> CloudWatch Settings -> Enable CloudWatch Logs
。然后,再次发送连接请求,并在CloudWatch中监视您的API日志。就我而言,我遇到了下一个错误:
由于配置错误,执行失败:API网关无权承担提供的角色{arn_of_my_role}
因此,我已经在角色的信任关系中添加了 API网关,正如提到的here并解决了该问题。