Setup a basic WebSocket mock in AWS ApiGateway

别等时光非礼了梦想. 提交于 2019-12-03 08:41:48

Using the following sources (below), I was able to get a quick and loose example AWS API Gateway connection working use MOCK endpoints with web sockets. I'm sure this can be flushed out more to provide an even better understanding of different request/response body messages and codes..

  1. First, go to API Gateway, click Create API
  2. For Choose the protocol, select WebSocket
  3. Give the API a name, and use the example $request.body.message for the route selection expression
  4. Click the Create API button
  5. Select the new API from the side pane, and click Routes
  6. Select the $connect route
  7. Under Route overview, select the Mock radio button for endpoint
  8. For the Request Template, enter an expression (using 200 for example).
  9. For Template Key, enter a key (using 200 for example)
  10. For the popup window on passthrough behavior, select No, Use Current Settings for now
  11. Click on the Template Key 200, and enter a template: {"statusCode" : 200} and then click the Save button.

  12. Select Route Overview to go back to the $connect overview page and then select the Add Integration Response button

  13. Create a response key of $default, which will reference the $default route under Routes

  14. For the $disconnect route, please repeat steps 7 - 13 for this specific route.

  15. Once done setting up the $disconnect route, please select the $default route under the Routes pane.

  16. Create an Integration Request the same way that was done for $connect and $disconnect (steps 7 - 11)
  17. For Integration Response under $default, we will be setting the route response up slightly different than $connect and $disconnect as those 2 routes' responses actually reference the $default response within their own respective Integration Responses.
  18. Create a response key of $default
  19. Enter a Template Selection Expression (using 200 for this example)
  20. Then create a Template Key (using 200 for this example)
  21. Click on the Template Key 200, and enter a template: {"statusCode" : 200, "connectionId" : "$context.connectionId"} and then click the Save button.

  22. {"connectionId" : "$context.connectionId"} was added to the response payload to show how variables can be used within the response to represent an actual value of the connection.
  23. Finally, under the Actions dropdown, select Deploy API, and enter a deployment stage name. This should generate a WebSocket URL that can now be connected to e.g. (wss://#######.execute-api.us-east-1.amazonaws.com/Test)
  24. Using wscat via cli, I am now able to connect to my WebSocket URL (see source below for installing wscat): wscat -c wss://#######.execute-api.us-east-1.amazonaws.com/Test

Sources:

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