首页 > Windows开发 > 详细

[AWS] WebSockets with API Gateway

时间:2021-05-18 23:08:38      阅读:25      评论:0      收藏:0      [点我收藏+]

User connects to API Gateway with some payload, Apigateway routing the request to different Lambda functions:

技术分享图片

 

技术分享图片

There are some special routes defined for websockets,

  • `$connect`: When user connect to API Gateway thought websocket
  • `$disconnet`:
  • `$defualt`: default one, when there is no matching route

 

API Gateway WebSocket URL

WebSocket API provides two URLs: WebSocket URL and Connection URL.

WebSocket URL:

  • Clinent use to connect to API
  • Allows clients to send message and receive notifications

Connection URL:

  • Send back message to client
  • Lambda function use it to send message
  • Requires a connection id to send a message to particular client

技术分享图片

Connection id will be stored in DynamoDB table, a lambda function will read from it.

 

Connection URL supports the following operations:

  • POST: to send a message to a client
  • GET: to get the latest connection status
  • DELETE: to disconnect a client from API

Here is an example of how to react to WebSocket events using Serverless Framework:

ConnectHandler:
    handler: src/websocket/connect.handler
    events:
      - websocket:
          route: $connect

  DisconnectHandler:
    handler: src/websocket/disconnect.handler
    events:
      - websocket:
          route: $disconnect

Installing Tool

npm install wscat -g
wscat -c wss://52zeaf29d.exeute-api.eu-central-1.amazonaws.com/dev

 

[AWS] WebSockets with API Gateway

原文:https://www.cnblogs.com/Answer1215/p/14782429.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!