

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 设置 FlexMatch 事件通知
<a name="match-notification"></a>

您可以使用事件通知来跟踪个人对战请求的状态。所有投入实际生产的游戏，或具有大量对战活动的预生产中的游戏都应使用事件通知。

有两个选项可用于设置事件通知。
+ 让您的对战构建器将事件通知发布到 Amazon Simple Notification Service 主题。
+ 使用自动发布的 Amazon EventBridge 事件及其工具套件来管理事件。

有关 Amazon GameLift Servers 发出的 FlexMatch 事件的列表，请参阅[FlexMatch 对战事件](match-events.md)。

**重要**  
对于高吞吐量对战系统，我们建议使用标准（非 FIFO）Amazon SNS 主题，而不是 FIFO 主题。FIFO 主题的发布限制低于标准主题，这可能会导致在高负载期间出现节流异常。如果您在使用 FIFO 主题时遇到节流，则可能会丢失 FlexMatch 通知。

**注意**  
 Amazon GameLift Servers使用内置的重试逻辑自动处理 Amazon SNS 交付失败和限制。当 Amazon SNS 返回限制错误或暂时失败时，会在两Amazon GameLift Servers次尝试之间逐渐延迟重试通知传送。这有助于确保可靠地发送事件通知。但是，如果在所有重试尝试后仍然失败，或者出现不可重试的错误（例如授权失败或缺少主题），则通知可能会丢失。

**Topics**
+ [设置 EventBridge 活动](#match-notification-cwe)
+ [教程：设置 Amazon SNS 主题](match-notification-sns.md)
+ [使用服务器端加密设置 Amazon SNS 主题](queue-notification-sns-sse.md)
+ [将主题订阅配置为调用 Lambda 函数](match-notification-lambda.md)

## 设置 EventBridge 活动
<a name="match-notification-cwe"></a>

Amazon GameLift Servers自动将所有配对活动发布到亚马逊 EventBridge。使用 EventBridge，您可以设置规则，将配对事件路由到目标进行处理。例如，您可以设置一条规则，将事件 “PotentialMatchCreated” 路由到处理玩家接受的 AWS Lambda 函数。有关更多信息，请参阅[什么是亚马逊 EventBridge？](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html)

**注意**  
在配置媒人时，请将通知目标字段留空，或者如果您想同时使用两者，请引用 SNS 主题 EventBridge 和 Amazon SNS。

# 教程：设置 Amazon SNS 主题
<a name="match-notification-sns"></a>

您可以让 Amazon GameLift Servers 将 FlexMatch 对战构建器生成的所有事件发布到 Amazon SNS 主题中。

**为 Amazon GameLift Servers 事件通知创建 SNS 主题**

1. 打开 [Amazon SNS 控制台](https://console.aws.amazon.com/sns)。

1. 在导航窗格中，选择**主题**。

1. 在 **Topics**（主页）页面上，选择 **Create topic**（创建主题）。

1. 在 控制台中，创建一个主题。有关更多信息，请参阅《Amazon Simple Notification Service 开发人员指南》**中的[创建主题 AWS 管理控制台](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html#create-topic-aws-console)。

1. 在主题的**详细信息**页面上，选择**编辑**。

1. （可选）在主题的**编辑**页面上，展开**访问策略**，然后将以下 AWS Identity and Access Management (IAM) 策略声明中的粗体语法添加到现有策略的末尾。(为清晰起见显示了整个策略。) 请务必将 Amazon 资源名称（ARN）详细信息用于您自己的 SNS 主题和 Amazon GameLift Servers 对战配置。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Id": "__default_policy_ID",
     "Statement": [
       {
         "Sid": "__default_statement_ID",
         "Effect": "Allow",
         "Principal": {
           "AWS": "*"
         },
         "Action": [
           "SNS:GetTopicAttributes",
           "SNS:SetTopicAttributes",
           "SNS:AddPermission",
           "SNS:RemovePermission",
           "SNS:DeleteTopic",
           "SNS:Subscribe",
           "SNS:ListSubscriptionsByTopic",
           "SNS:Publish"
         ],
         "Resource": "arn:aws:sns:us-east-1:111122223333:your_topic_name",
         "Condition": {
           "StringEquals": {
           "AWS:SourceAccount": "111122223333"
           }
         }
       },
       {
         "Sid": "__console_pub_0",
         "Effect": "Allow",
         "Principal": {
           "Service": "gamelift.amazonaws.com"
         },
         "Action": "SNS:Publish",
         "Resource": "arn:aws:sns:us-east-1:111122223333:your_topic_name",
         "Condition": {
           "ArnLike": {
           "aws:SourceArn": "arn:aws:gamelift:us-east-1:111122223333:matchmakingconfiguration/your_configuration_name"
           }
         }
       }
     ]
   }
   ```

------

1. 选择**保存更改**。

# 使用服务器端加密设置 Amazon SNS 主题
<a name="queue-notification-sns-sse"></a>

您可以使用服务器端加密 (SSE)，采用加密主题的方式传输敏感数据。SSE 使用在 AWS Key Management Service ()AWS KMS中管理的密钥保护 Amazon SNS 主题中的消息内容。有关 Amazon S3 如何执行加密的更多信息，请参阅 *Amazon Simple Storage Service 开发人员指南*中的[使用服务器端加密保护数据](https://docs.aws.amazon.com/sns/latest/dg/sns-server-side-encryption.html)。

要使用服务器端加密设置 SNS 主题，请查看下面的主题：
+ 《AWS Key Management Service 开发人员指南》**中的[创建密钥](https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html)。
+ 将 S *imple Notification Service 开发人员指南*[中的主题启用 SSE](https://docs.aws.amazon.com/sns/latest/dg/sns-enable-encryption-for-topic.html)

创建 KMS 密钥时，请使用以下 KMS 密钥策略：

```
{ 
  "Effect": "Allow", 
  "Principal": { 
    "Service": "gamelift.amazonaws.com" 
  },
  "Action": [
      "kms:Decrypt",
      "kms:GenerateDataKey"
  ],
  "Resource": "*",
  "Condition": {
      "ArnLike": { 
        "aws:SourceArn": "arn:aws:gamelift:your_region:your_account:matchmakingconfiguration/your_configuration_name" 
      },
      "StringEquals": { 
        "kms:EncryptionContext:aws:sns:topicArn": "arn:aws:sns:your_region:your_account:your_sns_topic_name" 
      }
  }
}
```

# 将主题订阅配置为调用 Lambda 函数
<a name="match-notification-lambda"></a>

您可以使用发布到 Amazon SNS 主题的事件通知调用 Lambda 函数。配置对战构建器时，将通知目标字段设置为 SNS 主题 ARN。

以下 AWS CloudFormation 模板将订阅名为的 SNS 主题配置为调用名`MyFlexMatchEventTopic`为的 Lambda 函数。`FlexMatchEventHandlerLambdaFunction`该模板创建了一个 IAM 权限策略，允许 Amazon GameLift Servers 写入 SNS 主题。然后，模板将调用 Lambda 函数的权限。

```
FlexMatchEventTopic:
  Type: "AWS::SNS::Topic"
  Properties:
    KmsMasterKeyId: alias/aws/sns #Enables server-side encryption on the topic using an AWS managed key 
    Subscription:
      - Endpoint: !GetAtt FlexMatchEventHandlerLambdaFunction.Arn
        Protocol: lambda
    TopicName: MyFlexMatchEventTopic

FlexMatchEventTopicPolicy:
  Type: "AWS::SNS::TopicPolicy"
  DependsOn: FlexMatchEventTopic
  Properties:
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Effect: Allow
          Principal:
            Service: gamelift.amazonaws.com
          Action:
            - "sns:Publish"
          Resource: !Ref FlexMatchEventTopic
    Topics:
      - Ref: FlexMatchEventTopic

FlexMatchEventHandlerLambdaPermission:
  Type: "AWS::Lambda::Permission"
  Properties:
    Action: "lambda:InvokeFunction"
    FunctionName: !Ref FlexMatchEventHandlerLambdaFunction
    Principal: sns.amazonaws.com
    SourceArn: !Ref FlexMatchEventTopic
```