I have an error calling AWS API Gateway Websocket Route using wscat or via node.js

六月ゝ 毕业季﹏ 提交于 2019-12-11 08:17:40

问题


I create a websocket and then a custom route. Before publishing I need to select an integration for $disconnect and $default, for both I choose Mock (I have also tried default Lambda functions), this allows me to publish.

I then use wscat to call

wscat -c wss://t0p5b2xpm3.execute-api.us-east-1.amazonaws.com/prod

the socket connects successfully,then i try to call the route

{"action":"echo", "data":"test response body"}

and get the following error.

{"message": "Internal server error", "connectionId":"aDH97cQJoAMCI8Q=", "requestId":"aDIAhGE8oAMFoEg="}

anyone have any ideas please?

thanks,

Matt


回答1:


For a lambda function, you need to return a statusCode of 200 for $connect, $disconnect, $default, and custom routes. A trivial javascript example:

module.exports.handler = async (event) => {
  return {
    statusCode: 200,
  };
};



回答2:


exports.handler = async (event) => {
  // TODO implement
  let time = "undefined";
  let state = "undefined";
  if (event.body !== null && event.body !== undefined) {
  let body = JSON.parse(event.body);
  if (body.time && body.state) 
      {
          time = body.time;
          state = body.state;
      }
  }
  const response = {
      statusCode: 200,
      body:   JSON.stringify({time:time, state: state}),
  };
  return response;
};

In case of API-Gateway you won't be able to modify the response as json as in Get and Post, so you can use this code to fetch time and state from the coming json object. Connect using wscat -c link and then send your json as {"time":"22:32","state":"LA"}

Try to put in $default to make sure that you are receiving the sending the right way.

Good luck!



来源:https://stackoverflow.com/questions/56245447/i-have-an-error-calling-aws-api-gateway-websocket-route-using-wscat-or-via-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!