私はJSクライアント用のWebソケットを通じて公開したいと思っている単純なマルチプレイヤーゲームのスケーラに取り組んでいます。Akka HTTP Websocket、アクター内部の接続を特定する方法
は、ここでこれはWebSocketのサーバーからメッセージを受信する必要があり、私の俳優のコードを簡略化されて
class WebsocketServer(actorRef: ActorRef, protocol: Protocol, system: ActorSystem, materializer: ActorMaterializer) extends Directives {
val route = get {
pathEndOrSingleSlash {
handleWebSocketMessages(websocketFlow)
}
}
def websocketFlow: Flow[Message, Message, Any] =
Flow[Message]
.map {
case TextMessage.Strict(textMessage) => protocol.hydrate(textMessage)
}
.via(actorFlow)
.map(event => TextMessage.Strict(protocol.serialize(event)))
def actorFlow : Flow[Protocol.Message, Protocol.Event, Any] = {
val sink =
Flow[Protocol.Message]
.to(Sink.actorRef[Protocol.Message](actorRef, Protocol.CloseConnection()))
val source =
Source.actorRef[Protocol.Event](1, OverflowStrategy.fail)
.mapMaterializedValue(actor => actorRef ! Protocol.OpenConnection(actor))
Flow.fromSinkAndSource(sink, source)
}
}
私WebsocketServerクラスです。
class GameActor() extends Actor {
private var connections: List[ActorRef] = List()
override def receive: Receive = {
case message: Protocol.OpenConnection => {
this.connections = message.connection :: this.connections
message.connection ! Protocol.ConnectionEstablished()
}
case message: Protocol.CloseConnection => {
// how can I remove actor from this.connections ?
}
case message: Protocol.DoSomething => {
// how can I identify from which connection this message came in?
}
}
}
これまでのところは良い、現在私は、しかし、私はまだ方法がわからないクライアントへの簡単なWelcomeMessageで応答することができるよ:接続リストから
- 削除俳優はいつでも俳優
CloseConnection
を受け取りますメッセージ? - どのメッセージから俳優にメッセージが届いたのかを特定しますか?
私はある種の仕組みを探していましたが、今までで最も簡単な解決策は考えていませんでした。 –