问题描述
我是 Websocket 世界的新手,我正在尝试实现多人游戏。
我真正想要的是将每个用户收到的消息分开。 更具体地说,假设我有两个 Player。 如果走棋有效,我想将新游戏状态发送给双方玩家,但如果走棋无效,我只想通知发送方。
你有什么提示吗?
我当前的实现可以将消息发送给他们两个:
private def wsUser(username: String): Flow[Message,Message,NotUsed] = {
// Create an actor for every WebSocket connection,this will represent the contact point to reach the user
val wsUser: ActorRef = system.actorOf(WebSocketUser.props(username))
// Integration point between Akka Streams and the above actor
val sink: Sink[Message,NotUsed] =
Flow[Message]
.collect { case TextMessage.Strict(json) => decode[Calculate](json) }
.filter(_.isRight)
.map(_.right.get)
.to(Sink.actorRef(wsUser,WsHandleDropped)) // connect to the wsUser Actor
// Integration point between Akka Streams and above actor
val source: Source[Message,NotUsed] =
Source
.actorRef(bufferSize = 10,overflowStrategy = OverflowStrategy.dropBuffer)
.map((c: Calculated) => TextMessage.Strict(c.asJson.noSpaces))
.mapMaterializedValue { wsHandle =>
// the wsHandle is the way to talk back to the user,our wsUser actor needs to kNow about this to send
// messages to the WebSocket user
wsUser ! ConnectWsHandle(wsHandle)
// don't expose the wsHandle anymore
NotUsed
}
.keepAlive(maxIdle = 10.seconds,() => TextMessage.Strict("Keep-alive message sent to WebSocket recipient"))
Flow.fromSinkAndSource(sink,source)
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)