doushi2902
2019-08-12 21:02
浏览 309
已采纳

为什么AWS API Gateway Websocket发送错误

I am using serverless framework to set up an AWS API Gateway websocket which calls a lambda function written in Go. Right now the handler only logs a message. I can connect to the websocket, and I can see the message is logged in Cloudwatch when I send a message through the socket, but I always get an error message that looks like

{
  "message": "Internal server error",
  "connectionId": "eU3C1cE7CYcCJPw=",
  "requestId": "eU3EQFX0iYcFysQ="
}

There are no errors logged for the lambda in Cloudwatch. The AWS API Gateway config looks good to me. I'm at a loss trying to think of what could cause this.

My main.go:

package main

import (
    "fmt"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func Handler(request events.APIGatewayWebsocketProxyRequest) {
    fmt.Println("default function ran")
}

func main() {
    lambda.Start(Handler)
}

Probably not super relevant but the serverless.yaml config:

functions:
  websocket-default:
    handler: bin/ws
    events:
      - websocket:
          route: $default

The payload I'm sending:

{
  "action": "whatever",
  "data":  "{whatever}"
}

图片转代码服务由CSDN问答提供 功能建议

我正在使用无服务器框架来设置一个AWS API Gateway Websocket,该Websocket调用用Go编写的lambda函数。 现在,处理程序仅记录一条消息。 我可以连接到网络套接字,并且可以看到通过套接字发送消息时该消息已记录在Cloudwatch中,但是我总是收到一条类似

 的错误消息 {
“ message”:“内部服务器错误”,
“ connectionId”:“ eU3C1cE7CYcCJPw =”,
“ requestId”:“ eU3EQFX0iYcFysQ =” 
} 
   
 \  n 

在Cloudwatch中没有为lambda记录任何错误。 AWS API Gateway配置对我来说很好。 我不知所措,想着是什么原因造成的。

我的main.go:

 包main 
 
import(
” fmt“ 
 
” github.com/aws/aws-lambda-go/events"
“ github.com/aws/aws-lambda-go  / lambda“ 
)
 
func处理程序(请求事件。APIGatewayWebsocketProxyRequest){
 fmt.Println(“默认函数已运行”)
} 
 
func main(){
 lambda.Start(Handler)\  n} 
   
 
 

可能不是超级相关,而是serverless.yaml配置:

 功能:
 websocket- 默认值:
处理程序:bin / ws 
事件:
-websocket:
路由:$ default 
   
 
 

我发送的有效负载:

  {
“ action”:“ whatever”,
“ data”:“ {whatever}” 
} 
   
 <  / DIV>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanran3115 2019-08-13 08:50
    已采纳

    Not sure if this is the only problem in your code, but your handler signature should be one listed in the doc:

    func ()
    func () error
    func (TIn) error
    func () (TOut, error)
    func (context.Context) error
    func (context.Context, TIn) error
    func (context.Context) (TOut, error)
    func (context.Context, TIn) (TOut, error)
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzhanlai4671 2019-08-13 16:04

    Alright so it took a lot of trial and error but this is the method signature that works

    type Response events.APIGatewayProxyResponse
    
    func Handler(context context.Context, request events.APIGatewayWebsocketProxyRequest) (Response, error) 
    

    I really wish the documentation for golang was better. Thanks to Clement for pointing me in the right direction.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题