本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
构建实时 WebSocket 客户端
Amazon AppSync的实时 WebSocket 客户端通过多步流程支持 GraphQL 订阅。客户端首先与 Amazon AppSync 实时端点建立 WebSocket 连接,发送连接初始化消息,然后等待确认。成功连接后,客户端通过发送带有唯一查询IDs和 GraphQL 查询的启动消息来注册订阅。 Amazon AppSync 使用确认消息确认订阅成功。然后,客户端监听订阅事件,这些事件由相应的突变触发。为了保持连接, Amazon AppSync 会定期发送保持活动状态的消息。完成后,客户端通过发送停止消息来取消订阅的注册。该系统支持在单个 WebSocket 连接上进行多个订阅,并支持各种授权模式,包括API密钥、Amazon Cognito 用户池和 Lamb IAM da。
Graph WebSocket QL 订阅的实时客户端实现
以下序列图和步骤显示了 WebSocket 客户端、HTTP客户端和之间的实时订阅工作流程 Amazon AppSync。
![](images/realtime-client-flow.png)
-
客户端与 Amazon AppSync 实时端点建立 WebSocket 连接。如果存在网络错误,则客户端应该执行抖动指数退避。有关更多信息,请参阅架构博客上的指数退避和抖动
。 Amazon -
成功建立 WebSocket 连接后,客户端发送一条
connection_init
消息。 -
客户端正在等待来自的
connection_ack
消息。 Amazon AppSync该消息包含一个connectionTimeoutMs
参数,它是"ka"
(保持活动)消息的最长等待时间(以毫秒为单位)。 -
Amazon AppSync 定期发送
"ka"
消息。客户端跟踪它收到每条"ka"
消息的时间。如果客户端在connectionTimeoutMs
毫秒内未收到"ka"
消息,客户端应关闭连接。 -
客户端通过发送
start
订阅消息来注册订阅。单个 WebSocket 连接支持多个订阅,即使它们处于不同的授权模式也是如此。 -
客户端等待发送
start_ack
消息 Amazon AppSync 以确认订阅成功。如果出现错误,则 Amazon AppSync 返回一条"type": "error"
消息。 -
客户端侦听订阅事件,这些事件是在调用相应变更后发送的。查询和突变通常会发送
https://
到 Amazon AppSync GraphQL 端点。订阅通过 secure WebSocket (wss://
) 流经 Amazon AppSync 实时端点。 -
客户端通过发送
stop
订阅消息来取消注册订阅。 -
取消注册所有订阅并检查没有消息通过传输后 WebSocket,客户端可以断开与 WebSocket 连接的连接。
握手详情以建立连接 WebSocket
要与连接并成功启动握手 Amazon AppSync, WebSocket 客户端需要满足以下条件:
-
Amazon AppSync 实时终端节点
-
包含
header
和payload
参数的查询字符串:-
header
:包含与 Amazon AppSync端点和授权相关的信息。这是来自字符串化对象的 base64 编码字符串。JSON根据授权模式的不同,JSON对象内容会有所不同。 -
payload
:payload
的 Base64 编码字符串。
-
根据这些要求, WebSocket 客户端可以使用graphql-ws
作为 WebSocket 协议连接到包含带有查询字符串的实时端点的。URL
从 GraphQL 终端节点发现 实时终端节点
Amazon AppSync GraphQL 端点和 Amazon AppSync 实时端点在协议和域上略有不同。您可以使用 Amazon Command Line Interface (Amazon CLI) 命令检索 GraphQL 端点。aws appsync
get-graphql-api
- Amazon AppSync GraphQL 端点:
-
https://example1234567890000.appsync-api.us-east-1.amazonaws.com/graphql
- Amazon AppSync 实时端点:
-
wss://example1234567890000.appsync-realtime-api.us-east-1.amazonaws.com/graphql
应用程序可以使用任何HTTP客户端连接到 Amazon AppSync GraphQL 端点 (https://
) 进行查询和变更。应用程序可以使用任何 WebSocket 客户端连接到 Amazon AppSync 实时终端节点 (wss://
) 进行订阅。
通过使用自定义域名,您可以使用单个域与两个终端节点进行交互。例如,如果您配置api.example.com
为自定义域,则可以使用以下方式与GraphQL和实时终端节点进行交互:URLs
- Amazon AppSync 自定义域 GraphQL 端点:
-
https://api.example.com/graphql
- Amazon AppSync 自定义域名实时终端节点:
-
wss://api.example.com/graphql/realtime
基于 Amazon AppSync API授权模式的标头参数格式
连接查询字符串中使用的header
对象的格式因 Amazon AppSync API授权模式而异。对象中的host
字段指的是 Amazon AppSync GraphQL 端点,即使wss://
调用是针对实时端点进行的,该端点也用于验证连接。要启动握手并建立授权连接,payload
应为空JSON对象。
API 密钥
标头内容
-
"host": <string>
: Amazon AppSyncGraphQL 终端节点的主机或您的自定义域名。 -
"x-api-key": <string>
:为配置的API密钥 Amazon AppSync API。
示例
{ "host":"example1234567890000.appsync-api.us-east-1.amazonaws.com", "x-api-key":"da2-12345678901234567890123456" }
负载内容
{}
请求 URL
wss://example1234567890000.appsync-realtime-api.us-east-1.amazonaws.com/graphql?header=eyJob3N0IjoiZXhhbXBsZTEyMzQ1Njc4OTAwMDAuYXBwc3luYy1hcGkudXMtZWFzdC0xLmFtYXpvbmF3cy5jb20iLCJ4LWFtei1kYXRlIjoiMjAyMDA0MDFUMDAxMDEwWiIsIngtYXBpLWtleSI6ImRhMi16NHc0NHZoczV6Z2MzZHRqNXNranJsbGxqaSJ9&payload=e30=
Amazon Cognito 用户池和 OpenID Connect () OIDC
标头内容:
-
"Authorization": <string>
: JWT ID 令牌。标头可以使用持有者方案。 -
"host": <string>
: Amazon AppSyncGraphQL 终端节点的主机或您的自定义域名。
例如:
{ "Authorization":"eyEXAMPLEiJjbG5xb3A5eW5MK09QYXIrMTJHWEFLSXBieU5WNHhsQjEXAMPLEnM2WldvPSIsImFsZyI6IlEXAMPLEn0.eyEXAMPLEiJhNmNmMjcwNy0xNjgxLTQ1NDItOWYxOC1lNjY0MTg2NjlkMzYiLCJldmVudF9pZCI6ImVkMzM5MmNkLWNjYTMtNGM2OC1hNDYyLTJlZGI3ZTNmY2FjZiIsInRva2VuX3VzZSI6ImFjY2VzcyIsInNjb3BlIjoiYXdzLmNvZ25pdG8uc2lnbmluLnVzZXIuYWRtaW4iLCJhdXRoX3RpbWUiOjE1Njk0NTc3MTgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5hcC1zb3V0aGVhc3QtMi5hbWF6b25hd3MuY29tXC9hcC1zb3V0aGVhc3QtMl83OHY0SVZibVAiLCJleHAiOjE1Njk0NjEzMjAsImlhdCI6MTU2OTQ1NzcyMCwianRpIjoiNTgzZjhmYmMtMzk2MS00YzA4LWJhZTAtYzQyY2IxMTM5NDY5IiwiY2xpZW50X2lkIjoiM3FlajVlMXZmMzd1N3RoZWw0dG91dDJkMWwiLCJ1c2VybmFtZSI6ImVsb3EXAMPLEn0.B4EXAMPLEFNpJ6ikVp7e6DRee95V6Qi-zEE2DJH7sHOl2zxYi7f-SmEGoh2AD8emxQRYajByz-rE4Jh0QOymN2Ys-ZIkMpVBTPgu-TMWDyOHhDUmUj2OP82yeZ3wlZAtr_gM4LzjXUXmI_K2yGjuXfXTaa1mvQEBG0mQfVd7SfwXB-jcv4RYVi6j25qgow9Ew52ufurPqaK-3WAKG32KpV8J4-Wejq8t0c-yA7sb8EnB551b7TU93uKRiVVK3E55Nk5ADPoam_WYE45i3s5qVAP_-InW75NUoOCGTsS8YWMfb6ecHYJ-1j-bzA27zaT9VjctXn9byNFZmEXAMPLExw", "host":"example1234567890000.appsync-api.us-east-1.amazonaws.com" }
负载内容:
{}
请求URL:
wss://example1234567890000.appsync-realtime-api.us-east-1.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoiZXlKcmFXUWlPaUpqYkc1eGIzQTVlVzVNSzA5UVlYSXJNVEpIV0VGTFNYQmllVTVXTkhoc1FqaFBWVzlZTW5NMldsZHZQU0lzSW1Gc1p5STZJbEpUTWpVMkluMC5leUp6ZFdJaU9pSmhObU5tTWpjd055MHhOamd4TFRRMU5ESXRPV1l4T0MxbE5qWTBNVGcyTmpsa016WWlMQ0psZG1WdWRGOXBaQ0k2SW1Wa016TTVNbU5rTFdOallUTXROR00yT0MxaE5EWXlMVEpsWkdJM1pUTm1ZMkZqWmlJc0luUnZhMlZ1WDNWelpTSTZJbUZqWTJWemN5SXNJbk5qYjNCbElqb2lZWGR6TG1OdloyNXBkRzh1YzJsbmJtbHVMblZ6WlhJdVlXUnRhVzRpTENKaGRYUm9YM1JwYldVaU9qRTFOamswTlRjM01UZ3NJbWx6Y3lJNkltaDBkSEJ6T2x3dlhDOWpiMmR1YVhSdkxXbGtjQzVoY0MxemIzVjBhR1ZoYzNRdE1pNWhiV0Y2YjI1aGQzTXVZMjl0WEM5aGNDMXpiM1YwYUdWaGMzUXRNbDgzT0hZMFNWWmliVkFpTENKbGVIQWlPakUxTmprME5qRXpNakFzSW1saGRDSTZNVFUyT1RRMU56Y3lNQ3dpYW5ScElqb2lOVGd6WmpobVltTXRNemsyTVMwMFl6QTRMV0poWlRBdFl6UXlZMkl4TVRNNU5EWTVJaXdpWTJ4cFpXNTBYMmxrSWpvaU0zRmxhalZsTVhabU16ZDFOM1JvWld3MGRHOTFkREprTVd3aUxDSjFjMlZ5Ym1GdFpTSTZJbVZzYjNKNllXWmxJbjAuQjRjZEp0aDNLRk5wSjZpa1ZwN2U2RFJlZTk1VjZRaS16RUUyREpIN3NIT2wyenhZaTdmLVNtRUdvaDJBRDhlbXhRUllhakJ5ei1yRTRKaDBRT3ltTjJZcy1aSWtNcFZCVFBndS1UTVdEeU9IaERVbVVqMk9QODJ5ZVozd2xaQXRyX2dNNEx6alhVWG1JX0syeUdqdVhmWFRhYTFtdlFFQkcwbVFmVmQ3U2Z3WEItamN2NFJZVmk2ajI1cWdvdzlFdzUydWZ1clBxYUstM1dBS0czMktwVjhKNC1XZWpxOHQwYy15QTdzYjhFbkI1NTFiN1RVOTN1S1JpVlZLM0U1NU5rNUFEUG9hbV9XWUU0NWkzczVxVkFQXy1Jblc3NU5Vb09DR1RzUzhZV01mYjZlY0hZSi0xai1iekEyN3phVDlWamN0WG45YnlORlptS0xwQTJMY3h3IiwiaG9zdCI6ImV4YW1wbGUxMjM0NTY3ODkwMDAwLmFwcHN5bmMtYXBpLnVzLWVhc3QtMS5hbWF6b25hd3MuY29tIn0=&payload=e30=
IAM
标头内容
-
"accept": "application/json, text/javascript"
:常量<string>
参数。 -
"content-encoding": "amz-1.0"
:常量<string>
参数。 -
"content-type": "application/json; charset=UTF-8"
:常量<string>
参数。 -
"host": <string>
:这是 Amazon AppSync GraphQL 端点的主机。-
"x-amz-date": <string>
: 时间戳必须采用以UTC下 ISO 8601 格式:YYYYMMDD'T' 'Z '。HHMMSS例如,20150830T123600Z 是一个有效的时间戳。请勿在时间戳中包含毫秒。有关更多信息,请参阅《Amazon Web Services 一般参考》中的处理签名版本 4 的日期。 -
"X-Amz-Security-Token": <string>
: Amazon 会话令牌,在使用临时安全凭证时,需要使用该令牌。有关更多信息,请参阅《IAM用户指南》中的将临时证书与 Amazon 资源配合使用。 -
"Authorization": <string>
: 终端节点的签名版本 4 (Sigv4) 签名信息。 Amazon AppSync有关签名过程的更多信息,请参阅中的任务 4:向HTTP请求添加签名Amazon Web Services 一般参考。
-
Sigv4 签名HTTP请求包括一个规范URL,即附加的 GraphQL 端点 Amazon AppSync 。/connect
服务终端节点 Amazon 区域与您使用的区域相同 Amazon AppSync API,服务名称为 “appsync”。签名HTTP请求如下:
{ url: "https://example1234567890000.appsync-api.us-east-1.amazonaws.com/graphql/connect", data: "{}", method: "POST", headers: { "accept": "application/json, text/javascript", "content-encoding": "amz-1.0", "content-type": "application/json; charset=UTF-8", } }
示例
{ "accept": "application/json, text/javascript", "content-encoding": "amz-1.0", "content-type": "application/json; charset=UTF-8", "host": "example1234567890000.appsync-api.us-east-1.amazonaws.com", "x-amz-date": "20200401T001010Z", "X-Amz-Security-Token": "AgEXAMPLEZ2luX2VjEAoaDmFwLXNvdXRoZWFEXAMPLEcwRQIgAh97Cljq7wOPL8KsxP3YtDuyc/9hAj8PhJ7Fvf38SgoCIQDhJEXAMPLEPspioOztj++pEagWCveZUjKEn0zyUhBEXAMPLEjj//////////8BEXAMPLExODk2NDgyNzg1NSIMo1mWnpESWUoYw4BkKqEFSrm3DXuL8w+ZbVc4JKjDP4vUCKNR6Le9C9pZp9PsW0NoFy3vLBUdAXEXAMPLEOVG8feXfiEEA+1khgFK/wEtwR+9zF7NaMMMse07wN2gG2tH0eKMEXAMPLEQX+sMbytQo8iepP9PZOzlZsSFb/dP5Q8hk6YEXAMPLEYcKZsTkDAq2uKFQ8mYUVA9EtQnNRiFLEY83aKvG/tqLWNnGlSNVx7SMcfovkFDqQamm+88y1OwwAEYK7qcoceX6Z7GGcaYuIfGpaX2MCCELeQvZ+8WxEgOnIfz7GYvsYNjLZSaRnV4G+ILY1F0QNW64S9Nvj+BwDg3ht2CrNvpwjVYlj9U3nmxE0UG5ne83LL5hhqMpm25kmL7enVgw2kQzmU2id4IKu0C/WaoDRuO2F5zE63vJbxN8AYs7338+4B4HBb6BZ6OUgg96Q15RA41/gIqxaVPxyTpDfTU5GfSLxocdYeniqqpFMtZG2n9d0u7GsQNcFkNcG3qDZm4tDo8tZbuym0a2VcF2E5hFEgXBa+XLJCfXi/77OqAEjP0x7Qdk3B43p8KG/BaioP5RsV8zBGvH1zAgyPha2rN70/tT13yrmPd5QYEfwzexjKrV4mWIuRg8NTHYSZJUaeyCwTom80VFUJXG+GYTUyv5W22aBcnoRGiCiKEYTLOkgXecdKFTHmcIAejQ9Welr0a196Kq87w5KNMCkcCGFnwBNFLmfnbpNqT6rUBxxs3X5ntX9d8HVtSYINTsGXXMZCJ7fnbWajhg/aox0FtHX21eF6qIGT8j1z+l2opU+ggwUgkhUUgCH2TfqBj+MLMVVvpgqJsPKt582caFKArIFIvO+9QupxLnEH2hz04TMTfnU6bQC6z1buVe7h+tOLnh1YPFsLQ88anib/7TTC8k9DsBTq0ASe8R2GbSEsmO9qbbMwgEaYUhOKtGeyQsSJdhSk6XxXThrWL9EnwBCXDkICMqdntAxyyM9nWsZ4bL9JHqExgWUmfWChzPFAqn3F4y896UqHTZxlq3WGypn5HHcem2Hqf3IVxKH1inhqdVtkryEiTWrI7ZdjbqnqRbl+WgtPtKOOweDlCaRs3R2qXcbNgVhleMk4IWnF8D1695AenU1LwHjOJLkCjxgNFiWAFEPH9aEXAMPLExA==", "Authorization": "AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXXXXXXXXX/20200401/us-east-1/appsync/aws4_request, SignedHeaders=accept;content-encoding;content-type;host;x-amz-date;x-amz-security-token, Signature=83EXAMPLEbcc1fe3ee69f75cd5ebbf4cb4f150e4f99cec869f149c5EXAMPLEdc" }
负载内容
{}
请求 URL
wss://example1234567890000.appsync-realtime-api.us-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xrQ2p4Z05GaVdBRkVQSDlhTklhcXMvWnhBPT0iLCJBdXRob3JpemF0aW9uIjoiQVdTNC1ITUFDLVNIQTI1NiBDcmVkZW50aWFsPVhYWFhYWFhYWFhYWFhYWFhYWFgvMjAxOTEwMDIvdXMtZWFzdC0xEXAMPLE5bmMvYXdzNF9yZXF1ZXN0LCBTaWduZWRIZWFkZXJzPWFjY2VwdDtjb250ZWEXAMPLE29kaW5nO2NvbnRlbnQtdHlwZTtob3EXAMPLEW16LWRhdGU7eC1hbXotc2VjdXJpdHktdG9rZW4sIFNpZ25hdHVyZT04MzE4EXAMPLEiY2MxZmUzZWU2OWY3NWNkEXAMPLE0Y2I0ZjE1MGU0Zjk5Y2VjODY5ZjE0OWM1ZDAzNDEXAMPLEn0=&payload=e30=
使用自定义域对请求进行签名:
{ url: "https://api.example.com/graphql/connect", data: "{}", method: "POST", headers: { "accept": "application/json, text/javascript", "content-encoding": "amz-1.0", "content-type": "application/json; charset=UTF-8", } }
示例
{ "accept": "application/json, text/javascript", "content-encoding": "amz-1.0", "content-type": "application/json; charset=UTF-8", "host": "api.example.com", "x-amz-date": "20200401T001010Z", "X-Amz-Security-Token": "AgEXAMPLEZ2luX2VjEAoaDmFwLXNvdXRoZWFEXAMPLEcwRQIgAh97Cljq7wOPL8KsxP3YtDuyc/9hAj8PhJ7Fvf38SgoCIQDhJEXAMPLEPspioOztj++pEagWCveZUjKEn0zyUhBEXAMPLEjj//////////8BEXAMPLExODk2NDgyNzg1NSIMo1mWnpESWUoYw4BkKqEFSrm3DXuL8w+ZbVc4JKjDP4vUCKNR6Le9C9pZp9PsW0NoFy3vLBUdAXEXAMPLEOVG8feXfiEEA+1khgFK/wEtwR+9zF7NaMMMse07wN2gG2tH0eKMEXAMPLEQX+sMbytQo8iepP9PZOzlZsSFb/dP5Q8hk6YEXAMPLEYcKZsTkDAq2uKFQ8mYUVA9EtQnNRiFLEY83aKvG/tqLWNnGlSNVx7SMcfovkFDqQamm+88y1OwwAEYK7qcoceX6Z7GGcaYuIfGpaX2MCCELeQvZ+8WxEgOnIfz7GYvsYNjLZSaRnV4G+ILY1F0QNW64S9Nvj+BwDg3ht2CrNvpwjVYlj9U3nmxE0UG5ne83LL5hhqMpm25kmL7enVgw2kQzmU2id4IKu0C/WaoDRuO2F5zE63vJbxN8AYs7338+4B4HBb6BZ6OUgg96Q15RA41/gIqxaVPxyTpDfTU5GfSLxocdYeniqqpFMtZG2n9d0u7GsQNcFkNcG3qDZm4tDo8tZbuym0a2VcF2E5hFEgXBa+XLJCfXi/77OqAEjP0x7Qdk3B43p8KG/BaioP5RsV8zBGvH1zAgyPha2rN70/tT13yrmPd5QYEfwzexjKrV4mWIuRg8NTHYSZJUaeyCwTom80VFUJXG+GYTUyv5W22aBcnoRGiCiKEYTLOkgXecdKFTHmcIAejQ9Welr0a196Kq87w5KNMCkcCGFnwBNFLmfnbpNqT6rUBxxs3X5ntX9d8HVtSYINTsGXXMZCJ7fnbWajhg/aox0FtHX21eF6qIGT8j1z+l2opU+ggwUgkhUUgCH2TfqBj+MLMVVvpgqJsPKt582caFKArIFIvO+9QupxLnEH2hz04TMTfnU6bQC6z1buVe7h+tOLnh1YPFsLQ88anib/7TTC8k9DsBTq0ASe8R2GbSEsmO9qbbMwgEaYUhOKtGeyQsSJdhSk6XxXThrWL9EnwBCXDkICMqdntAxyyM9nWsZ4bL9JHqExgWUmfWChzPFAqn3F4y896UqHTZxlq3WGypn5HHcem2Hqf3IVxKH1inhqdVtkryEiTWrI7ZdjbqnqRbl+WgtPtKOOweDlCaRs3R2qXcbNgVhleMk4IWnF8D1695AenU1LwHjOJLkCjxgNFiWAFEPH9aEXAMPLExA==", "Authorization": "AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXXXXXXXXX/20200401/us-east-1/appsync/aws4_request, SignedHeaders=accept;content-encoding;content-type;host;x-amz-date;x-amz-security-token, Signature=83EXAMPLEbcc1fe3ee69f75cd5ebbf4cb4f150e4f99cec869f149c5EXAMPLEdc" }
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VMUx3SGpPSkxrQ2p4Z05GaVdBRkVQSDlhTklhcXMvWnhBPT0iLCJBdXRob3JpemF0aW9uIjoiQVdTNC1ITUFDLVNIQTI1NiBDcmVkZW50aWFsPVhYWFhYWFhYWFhYWFhYWFhYWFgvMjAxOTEwMDIvdXMtZWFzdC0xEXAMPLE5bmMvYXdzNF9yZXF1ZXN0LCBTaWduZWRIZWFkZXJzPWFjY2VwdDtjb250ZWEXAMPLE29kaW5nO2NvbnRlbnQtdHlwZTtob3EXAMPLEW16LWRhdGU7eC1hbXotc2VjdXJpdHktdG9rZW4sIFNpZ25hdHVyZT04MzE4EXAMPLEiY2MxZmUzZWU2OWY3NWNkEXAMPLE0Y2I0ZjE1MGU0Zjk5Y2VjODY5ZjE0OWM1ZDAzNDEXAMPLEn0=&payload=e30=
注意
一个 WebSocket 连接可以有多个订阅(即使使用不同的身份验证模式也是如此)。实现这一点的一种方法是为第一个订阅创建一个 WebSocket 连接,然后在最后一个订阅取消注册时将其关闭。您可以通过等待几秒钟再关闭 WebSocket连接来进行优化,以防应用程序在最后一次订阅取消注册后立即订阅。以移动应用程序为例,从一个屏幕切换到另一个屏幕时,它在发生卸载事件时停止订阅,并在发生挂载事件时启动另一个订阅。
Lambda 授权
标头内容
-
"Authorization": <string>
:作为authorizationToken
传递的值。 -
"host": <string>
: Amazon AppSyncGraphQL 终端节点的主机或您的自定义域名。
示例
{ "Authorization":"M0UzQzM1MkQtMkI0Ni00OTZCLUI1NkQtMUM0MTQ0QjVBRTczCkI1REEzRTIxLTk5NzItNDJENi1BQjMwLTFCNjRFNzQ2NzlCNQo=", "host":"example1234567890000.appsync-api.us-east-1.amazonaws.com" }
负载内容
{}
请求 URL
wss://example1234567890000.appsync-realtime-api.us-east-1.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoiZXlKcmFXUWlPaUpqYkc1eGIzQTVlVzVNSzA5UVlYSXJNVEpIV0VGTFNYQmllVTVXTkhoc1FqaFBWVzlZTW5NMldsZHZQ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hFbkI1NTFiN1RVOTN1S1JpVlZLM0U1NU5rNUFEUG9hbV9XWUU0NWkzczVxVkFQXy1Jblc3NU5Vb09DR1RzUzhZV01mYjZlY0hZSi0xai1iekEyN3phVDlWamN0WG45YnlORlptS0xwQTJMY3h3IiwiaG9zdCI6ImV4YW1wbGUxMjM0NTY3ODkwMDAwLmFwcHN5bmMtYXBpLnVzLWVhc3QtMS5hbWF6b25hd3MuY29tIn0=&payload=e30=
实时 WebSocket操作
成功启动与的 WebSocket 握手后 Amazon AppSync,客户端必须发送后续消息以 Amazon AppSync 进行不同的操作。这些消息需要以下数据:
-
type
:操作的类型。 -
id
:订阅的唯一标识符。UUID为此,我们建议使用。 -
payload
:关联的负载,取决于操作类型。
type
字段是唯一的必填字段;id
和 payload
字段是可选的。
事件序列
要成功启动、建立、注册和处理订阅请求,客户端必须执行以下序列:
-
初始化连接 (
connection_init
) -
连接确认 (
connection_ack
) -
订阅注册 (
start
) -
订阅确认 (
start_ack
) -
处理订阅 (
data
) -
订阅取消注册 (
stop
)
连接启动消息
成功握手后,客户端必须发送connection_init
消息才能开始与 Amazon AppSync实时端点通信。如果没有执行该步骤,将忽略所有其他消息。该消息是通过对以下JSON对象进行字符串化而获得的字符串,如下所示:
{ "type": "connection_init" }
连接确认消息
发送 connection_init
消息后,客户端必须等待 connection_ack
消息。将忽略在收到 connection_ack
之前发送的所有消息。该消息具体如下:
{ "type": "connection_ack", "payload": { // Time in milliseconds waiting for ka message before the client should terminate the WebSocket connection "connectionTimeoutMs": 300000 } }
保持活动消息
除了连接确认消息以外,客户端还会定期收到保持活动消息。如果客户端在连接超时时间内没有收到保持活动状态的消息,则客户端应关闭连接。 Amazon AppSync 继续发送这些消息并为注册的订阅提供服务,直到它自动关闭连接(24 小时后)。保持活动消息是检测信号,客户端不需要确认这些消息。
{ "type": "ka" }
订阅注册消息
在客户端收到connection_ack
消息后,客户端可以向发送订阅注册消息 Amazon AppSync。这种类型的消息是一个包含以下字段的字符串化JSON对象:
-
"id": <string>
:订阅的 ID。对于每个订阅,该 ID 必须是唯一的,否则,服务器返回错误,以指示订阅 ID 是重复的。 -
"type": "start"
:常量<string>
参数。 -
"payload": <Object>
:包含与订阅相关的信息的对象。-
"data": <string>
:包含 GraphQL 查询和变量的字符串化JSON对象。-
"query": <string>
:GraphQL 操作。 -
"variables": <Object>
:包含查询变量的对象。
-
-
"extensions": <Object>
:包含授权对象的对象。
-
-
"authorization": <Object>
:包含授权所需的字段的对象。
订阅注册的授权对象
基于 Amazon AppSync API授权模式的标头参数格式一节中的相同规则适用于授权对象。唯一的例外是IAM,其中 Sigv4 签名信息略有不同。有关更多详细信息,请参阅示IAM例。
使用 Amazon Cognito 用户池的示例:
{ "id": "ee849ef0-cf23-4cb8-9fcb-152ae4fd1e69", "payload": { "data": "{\"query\":\"subscription onCreateMessage {\\n onCreateMessage {\\n __typename\\n message\\n }\\n }\",\"variables\":{}}", "extensions": { "authorization": { "Authorization": "eyEXAMPLEiJjbG5xb3A5eW5MK09QYXIrMTJEXAMPLEBieU5WNHhsQjhPVW9YMnM2WldvPSIsImFsZyI6IlEXAMPLEn0.eyJzdWIiOiJhNmNmMjcwNy0xNjgxLTQ1NDItEXAMPLENjY0MTg2NjlkMzYiLCJldmVudF9pZCI6ImU3YWVmMzEyLWUEXAMPLEY0Zi04YjlhLTRjMWY5M2Q5ZTQ2OCIsInRva2VuX3VzZSI6ImFjY2VzcyIsIEXAMPLEIjoiYXdzLmNvZ25pdG8uc2lnbmluLnVzZXIuYWRtaW4iLCJhdXRoX3RpbWUiOjE1Njk2MTgzMzgsImlzcyI6Imh0dEXAMPLEXC9jb2duaXRvLWlkcC5hcC1zb3V0aGVhc3QtMi5hbWF6b25hd3MuY29tXC9hcC1zbEXAMPLEc3QtMl83OHY0SVZibVAiLCJleHAiOjE1NzAyNTQ3NTUsImlhdCI6MTU3MDI1MTE1NSwianRpIjoiMmIEXAMPLEktZTVkMi00ZDhkLWJiYjItNjA0YWI4MDEwOTg3IiwiY2xpZW50X2lkIjoiM3FlajVlMXZmMzd1EXAMPLE0dG91dDJkMWwiLCJ1c2VybmFtZSI6ImVsb3J6YWZlIn0.CT-qTCtrYeboUJ4luRSTPXaNewNeEXAMPLE14C6sfg05tO0fOMpiUwj9k19gtNCCMqoSsjtQoUweFnH4JYa5EXAMPLEVxOyQEQ4G7jQrt5Ks6STn53vuseR3zRW9snWgwz7t3ZmQU-RWvW7yQU3sNQRLEXAMPLEcd0yufBiCYs3dfQxTTdvR1B6Wz6CD78lfNeKqfzzUn2beMoup2h6EXAMPLE4ow8cUPUPvG0DzRtHNMbWskjPanu7OuoZ8iFO_Eot9kTtAlVKYoNbWkZhkD8dxutyoU4RSH5JoLAnrGF5c8iKgv0B2dfEXAMPLEIihxaZVJ9w9w48S4EXAMPLEcA", "host": "example1234567890000.appsync-api.us-east-1.amazonaws.com" } } }, "type": "start" }
使用示例IAM:
{ "id": "eEXAMPLE-cf23-1234-5678-152EXAMPLE69", "payload": { "data": "{\"query\":\"subscription onCreateMessage {\\n onCreateMessage {\\n __typename\\n message\\n }\\n }\",\"variables\":{}}", "extensions": { "authorization": { "accept": "application/json, text/javascript", "content-type": "application/json; charset=UTF-8", "X-Amz-Security-Token": "AgEXAMPLEZ2luX2VjEAoaDmFwLXNvdXRoZWFEXAMPLEcwRQIgAh97Cljq7wOPL8KsxP3YtDuyc/9hAj8PhJ7Fvf38SgoCIQDhJEXAMPLEPspioOztj++pEagWCveZUjKEn0zyUhBEXAMPLEjj//////////8BEXAMPLExODk2NDgyNzg1NSIMo1mWnpESWUoYw4BkKqEFSrm3DXuL8w+ZbVc4JKjDP4vUCKNR6Le9C9pZp9PsW0NoFy3vLBUdAXEXAMPLEOVG8feXfiEEA+1khgFK/wEtwR+9zF7NaMMMse07wN2gG2tH0eKMEXAMPLEQX+sMbytQo8iepP9PZOzlZsSFb/dP5Q8hk6YEXAMPLEYcKZsTkDAq2uKFQ8mYUVA9EtQnNRiFLEY83aKvG/tqLWNnGlSNVx7SMcfovkFDqQamm+88y1OwwAEYK7qcoceX6Z7GGcaYuIfGpaX2MCCELeQvZ+8WxEgOnIfz7GYvsYNjLZSaRnV4G+ILY1F0QNW64S9Nvj+BwDg3ht2CrNvpwjVYlj9U3nmxE0UG5ne83LL5hhqMpm25kmL7enVgw2kQzmU2id4IKu0C/WaoDRuO2F5zE63vJbxN8AYs7338+4B4HBb6BZ6OUgg96Q15RA41/gIqxaVPxyTpDfTU5GfSLxocdYeniqqpFMtZG2n9d0u7GsQNcFkNcG3qDZm4tDo8tZbuym0a2VcF2E5hFEgXBa+XLJCfXi/77OqAEjP0x7Qdk3B43p8KG/BaioP5RsV8zBGvH1zAgyPha2rN70/tT13yrmPd5QYEfwzexjKrV4mWIuRg8NTHYSZJUaeyCwTom80VFUJXG+GYTUyv5W22aBcnoRGiCiKEYTLOkgXecdKFTHmcIAejQ9Welr0a196Kq87w5KNMCkcCGFnwBNFLmfnbpNqT6rUBxxs3X5ntX9d8HVtSYINTsGXXMZCJ7fnbWajhg/aox0FtHX21eF6qIGT8j1z+l2opU+ggwUgkhUUgCH2TfqBj+MLMVVvpgqJsPKt582caFKArIFIvO+9QupxLnEH2hz04TMTfnU6bQC6z1buVe7h+tOLnh1YPFsLQ88anib/7TTC8k9DsBTq0ASe8R2GbSEsmO9qbbMwgEaYUhOKtGeyQsSJdhSk6XxXThrWL9EnwBCXDkICMqdntAxyyM9nWsZ4bL9JHqExgWUmfWChzPFAqn3F4y896UqHTZxlq3WGypn5HHcem2Hqf3IVxKH1inhqdVtkryEiTWrI7ZdjbqnqRbl+WgtPtKOOweDlCaRs3R2qXcbNgVhleMk4IWnF8D1695AenU1LwHjOJLkCjxgNFiWAFEPH9aEXAMPLExA==", "Authorization": "AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXXXXXXXXXX/20200401/us-east-1/appsync/aws4_request, SignedHeaders=accept;content-encoding;content-type;host;x-amz-date;x-amz-security-token, Signature=b90131a61a7c4318e1c35ead5dbfdeb46339a7585bbdbeceeaff51f4022eb1fd", "content-encoding": "amz-1.0", "host": "example1234567890000.appsync-api.us-east-1.amazonaws.com", "x-amz-date": "20200401T001010Z" } } }, "type": "start" }
使用自定义域名的示例:
{ "id": "key-cf23-4cb8-9fcb-152ae4fd1e69", "payload": { "data": "{\"query\":\"subscription onCreateMessage {\\n onCreateMessage {\\n __typename\\n message\\n }\\n }\",\"variables\":{}}", "extensions": { "authorization": { "x-api-key": "da2-12345678901234567890123456", "host": "api.example.com" } } }, "type": "start" }
无需将 Sigv4 签名附加/connect
到中,字符串化的 Gr JSON aph URL QL 操作会替换。data
以下是一个 SigV4 签名请求示例:
{ url: "https://example1234567890000.appsync-api.us-east-1.amazonaws.com/graphql", data: "{\"query\":\"subscription onCreateMessage {\\n onCreateMessage {\\n __typename\\n message\\n }\\n }\",\"variables\":{}}", method: "POST", headers: { "accept": "application/json, text/javascript", "content-encoding": "amz-1.0", "content-type": "application/json; charset=UTF-8", } }
订阅确认消息
发送订阅开始消息后,客户端应等待 Amazon AppSync发送start_ack
消息。start_ack
消息表示订阅成功。
订阅确认示例:
{ "type": "start_ack", "id": "eEXAMPLE-cf23-1234-5678-152EXAMPLE69" }
错误消息
如果连接启动或订阅注册失败,或者从服务器中终止订阅,服务器将向客户端发送错误消息:
-
"type": "error"
:常量<string>
参数。 -
"id": <string>
:注册的相应订阅的 ID(如果相关)。 -
"payload" <Object>
:包含相应错误信息的对象。
例如:
{ "type": "error", "payload": { "errors": [ { "errorType": "LimitExceededError", "message": "Rate limit exceeded" } ] } }
处理数据消息
当客户端提交突变时,会 Amazon AppSync 识别所有对此变更感兴趣的订阅者,并使用订阅操作中的相应订阅向每个订阅id
者"start"
发送"type":"data"
消息。客户端应跟踪它发送的订阅 id
,这样,在它收到数据消息时,客户端可以将其与相应的订阅进行匹配。
-
"type": "data"
:常量<string>
参数。 -
"id": <string>
:注册的相应订阅的 ID。 -
"payload" <Object>
:包含订阅信息的对象。
例如:
{ "type": "data", "id": "ee849ef0-cf23-4cb8-9fcb-152ae4fd1e69", "payload": { "data": { "onCreateMessage": { "__typename": "Message", "message": "test" } } } }
订阅取消注册消息
当应用程序想要停止监听订阅事件时,客户端应发送一条包含以下字符串化JSON对象的消息:
-
"type": "stop"
:常量<string>
参数。 -
"id": <string>
:要取消注册的订阅的 ID。
例如:
{ "type":"stop", "id":"ee849ef0-cf23-4cb8-9fcb-152ae4fd1e69" }
Amazon AppSync 发送带有以下字符串化JSON对象的确认消息:
-
"type": "complete"
:常量<string>
参数。 -
"id": <string>
:取消注册的订阅的 ID。
在客户端收到确认消息后,它不会接收该特定订阅的更多消息。
例如:
{ "type":"complete", "id":"eEXAMPLE-cf23-1234-5678-152EXAMPLE69" }
断开连接 WebSocket
在断开连接之前,为避免数据丢失,客户端应具有必要的逻辑来检查当前是否未通过 WebSocket连接进行任何操作。在断开与的连接之前,应取消注册所有订阅。 WebSocket