筛选 AppSync 订阅
Filter AppSync Subscription
我正在使用 AppSync(以避免必须从零开始管理 WebSocket)和 Amplify(特别是在 Android,使用 Kotlin)构建一个简单的多人游戏。
问题是当玩家连接到游戏时,应该监听其他玩家的更新(考虑到我在 dynamo 上有一个 table 主键 UUID 和相应的游戏 ID 和玩家的位置)。
所以我所做的是,在玩家加入他的游戏会话后,将自己注册到 onUpdate
of Player
...但是现在他收到了所有更新的通知播放器...即使我可以轻松地过滤客户端传入的事件,我认为这是一个糟糕的主意,因为 AppSync 必须通知所有注册用户所有更新。
相反,我想在订阅上指定一个过滤器,比如当您执行查询时,例如:
Amplify.API.subscribe(
ModelSubscription.onUpdate(
Player::class.java,
Player.GAMEROOM.eq(currentPlayer.gameroom.id) // only players in the same gameroom
),
{ Log.i("ApiQuickStart", "Subscription established") },
{ onCreated ->
...
},
{ onFailure -> Log.e("ApiQuickStart", "Subscription failed", onFailure) },
{ Log.i("ApiQuickStart", "Subscription completed") }
)
有没有办法得到这个?
由于这是我将对 Player Update 进行的唯一订阅方式,也许使用自定义解析器可以完成一些事情?
您需要在您的订阅及其订阅的突变中再添加一个参数。我猜你的 GraphQL 架构看起来像
type Subscription {
onUpdate(gameRoomId: ID!, playerId: ID): UpdateUserResult
@aws_subscribe(mutations: ["leaveCurrentGameRoom", <other_interested_mutation>])
}
type Mutation {
leaveCurrentGameRoom: UpdateUserResult
}
type UpdateUserResult {
gameRoomId: ID!
playerId: ID!
// other fields
}
您注意到 Subscription.onUpdate 有一个可选参数 playerId。订阅方式如下
onUpdate("room-1") // AppSync gives updates of all players in "room-1"
onUpdate("room-1", "player-A") // AppSync gives updates of "player-A" in "room-1"
为了让 AppSync 过滤掉不相关的更新,订阅的变更(例如 leaveCurrentGameRoom)必须 return 包含两个字段 gameRoomId 和 playerId 的类型。请注意,您还可以更新突变的解析器以解析字段的数据
这是订阅参数的另一个例子
https://docs.aws.amazon.com/appsync/latest/devguide/aws-appsync-real-time-data.html#using-subscription-arguments
我正在使用 AppSync(以避免必须从零开始管理 WebSocket)和 Amplify(特别是在 Android,使用 Kotlin)构建一个简单的多人游戏。
问题是当玩家连接到游戏时,应该监听其他玩家的更新(考虑到我在 dynamo 上有一个 table 主键 UUID 和相应的游戏 ID 和玩家的位置)。
所以我所做的是,在玩家加入他的游戏会话后,将自己注册到 onUpdate
of Player
...但是现在他收到了所有更新的通知播放器...即使我可以轻松地过滤客户端传入的事件,我认为这是一个糟糕的主意,因为 AppSync 必须通知所有注册用户所有更新。
相反,我想在订阅上指定一个过滤器,比如当您执行查询时,例如:
Amplify.API.subscribe(
ModelSubscription.onUpdate(
Player::class.java,
Player.GAMEROOM.eq(currentPlayer.gameroom.id) // only players in the same gameroom
),
{ Log.i("ApiQuickStart", "Subscription established") },
{ onCreated ->
...
},
{ onFailure -> Log.e("ApiQuickStart", "Subscription failed", onFailure) },
{ Log.i("ApiQuickStart", "Subscription completed") }
)
有没有办法得到这个?
由于这是我将对 Player Update 进行的唯一订阅方式,也许使用自定义解析器可以完成一些事情?
您需要在您的订阅及其订阅的突变中再添加一个参数。我猜你的 GraphQL 架构看起来像
type Subscription {
onUpdate(gameRoomId: ID!, playerId: ID): UpdateUserResult
@aws_subscribe(mutations: ["leaveCurrentGameRoom", <other_interested_mutation>])
}
type Mutation {
leaveCurrentGameRoom: UpdateUserResult
}
type UpdateUserResult {
gameRoomId: ID!
playerId: ID!
// other fields
}
您注意到 Subscription.onUpdate 有一个可选参数 playerId。订阅方式如下
onUpdate("room-1") // AppSync gives updates of all players in "room-1"
onUpdate("room-1", "player-A") // AppSync gives updates of "player-A" in "room-1"
为了让 AppSync 过滤掉不相关的更新,订阅的变更(例如 leaveCurrentGameRoom)必须 return 包含两个字段 gameRoomId 和 playerId 的类型。请注意,您还可以更新突变的解析器以解析字段的数据
这是订阅参数的另一个例子 https://docs.aws.amazon.com/appsync/latest/devguide/aws-appsync-real-time-data.html#using-subscription-arguments