Skip to content

Instantly share code, notes, and snippets.

@LukasGasior1
Created May 28, 2015 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukasGasior1/71905a731b7e3ffae9f1 to your computer and use it in GitHub Desktop.
Save LukasGasior1/71905a731b7e3ffae9f1 to your computer and use it in GitHub Desktop.
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