blob: f10f92550dd5cd0ae7a14b3f114a740ff147708a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package controllers
import akka.actor.{ Actor, ActorRef, Terminated }
import chat._
import scala.collection.mutable.HashMap
/** An actor instantiated for every websocket connection. It represents a
* chat client and registers with a chat room.
* @param uid user id of the connecting client
* @param room chat room actor that this client will join
* @param socket websocket actor, any message sent to it will get transferred to the remote
* browser
*/
class ClientActor(uid: String, room: ActorRef, socket: ActorRef) extends Actor {
override def preStart() = {
room ! (self, uid)
}
def receive = {
case cmd: Command => room ! cmd
case ev: Event => socket ! ev
}
}
/** An actor that represents a chat room.
* Handles commands and events subclassing `chat.Command` and `chat.Event`
*/
class RoomActor extends Actor {
val clients = new HashMap[ActorRef, String]
override def receive = {
case (client: ActorRef, uid: String) =>
context.watch(sender)
clients += client -> uid
for ( (client, _) <- clients ) {
client ! Joined(uid)
}
case Terminated(client) =>
clients -= client
for ( (cl, _) <- clients ) {
cl ! Left(clients(client))
}
case Broadcast(content) =>
val origin = clients(sender)
for ( (client, _) <- clients ) {
client ! Message(origin, content)
}
}
}
|