Scala,Play,Akka,Websocket:如何通过websocket传递actor消息 [英] Scala, Play, Akka, Websocket: how to pass actor messages through websocket
问题描述
我有一个随应用程序一起启动的actor,它在后台运行以监视某些更改以及是否有任何报告.目前,它只是控制台的println
.我需要做的是每当有新消息时-使用Websocket将其发送到前端.
I have an actor which is launched with application, running in the background watching for certain changes and if there are any reporting them. At the moment it just a println
to the console. What I need to do is whenever there is a new message - send it to the front end using Websocket.
这是我的Play Global对象,在其中启动了监视/侦听演员:
This is my Play Global object where the monitoring/listening actor is launched:
object Global extends GlobalSettings {
override def onStart(app: Application) {
class Listener extends Actor {
//This needs to be changed to pass messages to Websocket, how?
def receive = {
case Create(path) => println("CREATE " + path)
case Delete(path) => println("DELETE " + path)
case Modify(path) => println("MODIFY " + path)
}
}
val listener = Akka.system.actorOf(Props[Listener], "listener")
val swatch = Akka.system.actorOf(Props[SwatchActor], "swatch")
swatch ! Watch("/folder/path", Seq(Create, Modify, Delete), true, Some(listener))
}
}
这是我的Play控制器:
This is my Play controller:
object Application extends Controller {
def test = WebSocket.using[String] { request =>
//This hopefully gets the listener actor reference?
val listener = Akka.system.actorSelection("/user/listener")
val (out, channel) = Concurrent.broadcast[String]
val in = Iteratee.foreach[String] { msg =>
//Actor messages must be pushed here, how?
channel push("RESPONSE: " + msg)
}
(in, out)
}
}
我了解到,要建立Websocket连接,必须有一个初始的输入".
I understand that in order for websocket connection to be established there has to be an initial "in".
所以我的问题是:
- 如何修改
Listener
actor以将消息推送到Websocket? - 建立websocket连接后,我需要做些什么来准备actor推送消息?
- 如何将消息从侦听器参与者推送到网络套接字?
- How do I modify the
Listener
actor to push messages to Websocket? - What do I need to do to prepare the actor to push messages once the websocket connection is established?
- How do I push messages from the listener actor to the websocket?
推荐答案
我找到了解决方案.
必须从单独的文件中导入的案例类:
Case class that has to be imported from a separate file:
case class Start(out: Concurrent.Channel[String])
全局对象:
object Global extends GlobalSettings {
override def onStart(app: Application) {
class Listener extends Actor {
var out = {
val (enum, chan) = Concurrent.broadcast[String]
chan
}
def receive = {
//Websocket channel out is set here
case Start(out) => this.out = out
//Pushing messages to Websocket
case Create(path) => this.out.push(path.toString)
case Delete(path) => this.out.push(path.toString)
case Modify(path) => this.out.push(path.toString)
}
}
val listener = Akka.system.actorOf(Props[Listener], "listener")
val swatch = Akka.system.actorOf(Props[SwatchActor], "swatch")
swatch ! Watch("/folder/path", Seq(Create, Modify, Delete), true, Option(listener))
}
}
播放控制器:
object Application extends Controller {
def test = WebSocket.using[String] { request =>
val (out, channel) = Concurrent.broadcast[String]
val listener = Akka.system.actorSelection("akka://application/user/listener")
//This is where the websocket out channel is being passed to the listener actor
listener ! Start(channel)
val in = Iteratee.foreach[String] { msg =>
channel push("RESPONSE: " + msg)
}
(in, out)
}
}
这篇关于Scala,Play,Akka,Websocket:如何通过websocket传递actor消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!