我正在尝试使用play 2.3.x实现websocket。我希望服务器能够向一个客户端或所有客户端发送消息。 这是我的控制器代码:
object Client extends Controller {
def socket(guid: String) = WebSocket.acceptWithActor[String, String] { request =>
out => ClientWebSocketActor.props(guid, out)
}
以下是我的演员代码:
object ClientWebSocketActor {
def props(uuid: String, out: ActorRef) = Props(new ClientWebSocketActor(uuid, out))
}
class ClientWebSocketActor(uuid: String, out: ActorRef) extends Actor {
val (enumerator, channel) = Concurrent.broadcast[String]
var clients = Map[String, (Enumerator[String], Channel[String])]()
import ClientWebSocketActor._
def receive = {
case msg: String =>
println(msg)
val (enumerator, channel1) = Concurrent.broadcast[String]
clients += ((uuid, (enumerator, channel1)))
println(clients)
channel1 push "Message is " + msg
}
}
我面临的问题:
非常感谢任何帮助。