Created
May 28, 2015 10:31
-
-
Save LukasGasior1/71905a731b7e3ffae9f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WebsocketEventPublisher(gameId: String, out: ActorRef) | |
extends Actor | |
with ActorLogging | |
with ImplicitFlowMaterializer { | |
import context.dispatcher | |
override def preStart() = { | |
import Global.connection | |
import Config.Events._ | |
val queue = Queue(name = gameId, durable = false, autoDelete = true) | |
val bindFuture = for { | |
_ <- connection.queueDeclare(queue) | |
_ <- connection.queueBind(queue.name, exchangeName, "", Map("gameId" -> gameId)) | |
} yield () | |
bindFuture.map { _ => | |
Source(connection.consume(queue.name)) | |
.map(_.message) | |
.to(Sink(EventSubscriber.props(self))) | |
.run() | |
}.failed.map { ex => | |
log.error(ex, "Cannot bind queue to events from game {}", gameId) | |
context stop self | |
} | |
} | |
override def receive = { | |
case ev: GameEvent if ev.gameId == gameId => | |
out ! ev | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment