-
-
Save crockpotveggies/3757237 to your computer and use it in GitHub Desktop.
Akka MessageBus with WebSockets access wrapped in Actors
This file contains hidden or 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
/** | |
* actor wrapping access for browser socket | |
*/ | |
class BrowserSocket( | |
val s: WebSocketConnection, | |
val userId: Long, | |
val teamId: Long | |
) extends Actor { | |
def receive = { | |
case MessageEvent(channel,message) => | |
s.send(message) | |
} | |
} |
This file contains hidden or 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
import akka.event.ActorEventBus | |
import akka.event.LookupClassification | |
import akka.actor._ | |
case class MessageEvent(val channel:String, val message:String) | |
/** | |
* message bus to route messages to their appropriate contexts | |
*/ | |
class MessageBus extends ActorEventBus with LookupClassification { | |
type Event = MessageEvent | |
type Classifier = String | |
protected val mapSize(): Int = { | |
10 | |
} | |
protected def classify(event: Event): Classifier = { | |
event.channel | |
} | |
protected def publish(event: Event, subscriber: Subscriber): Unit = { | |
subscriber ! event | |
} | |
} | |
object MessageBus { | |
val actorSystem = ActorSystem("contexts") | |
val Bus = new MessageBus | |
/** | |
* create an actor that stores a browser socket | |
*/ | |
def browserSocketContext(s: WebSocketConnection, userId: Long, teamId: Long) = { | |
val subscriber = actorSystem.actorOf(Props(new BrowserSocket(s,userId,teamId))) | |
Bus.subscribe( subscriber, "/app/socket/%s" format s.toString) | |
Bus.subscribe( subscriber, "/app/browser/u/%s" format userId ) | |
Bus.subscribe( subscriber, "/app/browser/t/%s" format teamId ) | |
Bus.subscribe( subscriber, "/app/browser" ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment