daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Feb 19th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. val socketActor = system.actorOf(Props(new Actor {
  2.   val socket = WebSocketConnection
  3.  
  4.   def receive = {
  5.     case d: AppMessage ⇒ socket.send(d)
  6.   }
  7. }))
  8. system.eventStream.subscribe(socketActor, classOf[AppMessage])
  9.    
  10. system.eventStream.subscribe(socketActor, Map("userId" -> userId, "teamId" -> teamId) )
  11.    
  12. case class MessageEvent(val channel:String, val message:String)
  13.  
  14. /**
  15.  * message bus to route messages to their appropriate contexts
  16.  */
  17. class MessageBus extends ActorEventBus with LookupClassification {
  18.  
  19.     type Event = MessageEvent
  20.   type Classifier = String
  21.  
  22.   protected def mapSize(): Int = {
  23.     10
  24.   }
  25.  
  26.   protected def classify(event: Event): Classifier = {
  27.     event.channel
  28.   }
  29.  
  30.   protected def publish(event: Event, subscriber: Subscriber): Unit = {
  31.     subscriber ! event
  32.   }
  33.  
  34. }
  35.  
  36.  
  37. object MessageBus {
  38.  
  39.   val actorSystem = ActorSystem("contexts")
  40.   val Bus = new MessageBus
  41.  
  42.   /**
  43.    * create an actor that stores a browser socket
  44.    */
  45.   def browserSocketContext(s: WebSocketConnection, userId: Long, teamId: Long) = {
  46.     val subscriber = actorSystem.actorOf(Props(new BrowserSocket(s,userId,teamId)))
  47.  
  48.     Bus.subscribe( subscriber, "/app/socket/%s" format s.toString)
  49.     Bus.subscribe( subscriber, "/app/browser/u/%s" format userId )
  50.     Bus.subscribe( subscriber, "/app/browser/t/%s" format teamId )
  51.     Bus.subscribe( subscriber, "/app/browser" )
  52.   }
  53. }
  54.    
  55. /**
  56.  * actor wrapping access for browser socket
  57.  */
  58. class BrowserSocket(
  59.   val s: WebSocketConnection,
  60.   val userId: Long,
  61.   val teamId: Long
  62.  
  63. ) extends Actor {
  64.  
  65.   def receive = {
  66.     case payload:MessageEvent =>
  67.       s.send(payload.message)
  68.  
  69.     case ping:MessagePing =>
  70.       s.ping(ping.data)
  71.  
  72.   }
  73.  
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top