问题
I'm attempting to retrieve a url parameter within a CometActor to validate that the source of the data matches the destination, e.g A user sends a message from room A, which should be received and displayed only in room A, not B or C.
I've tried:
S.param("message").openOr("")
But it's always empty, can this be done? Or is there another way to stop Comet messages going where they shouldn't?
Thanks in advance for any help, much appreciated :)
回答1:
CometActors exist outside of the session and so don't have access to (most of) it. The solution is to initialize the actor with an initialization message containing the desired session data. There's some sort of helper, perhaps in LiftRules, to do that. I'm on my phone and recounting this from memory but hopefully it's enough to go on.
Specifically, you're going to want to do something like:
for (
session <- S.session
message <- S.param("message")
) {
session.setupComet("myCometActor", Some("unique name, if you want it"), message)
}
in your Boot.scala
.
Check out LiftSession for a little more. I think there might be a way to hook into LiftRules to have the relevant code called upon session creation...
Update: And here's what your CometActor might look like if we send a case class containing:
// ...
session.setupComet(
"myCometActor",
Some("unique name, if you want it"),
Message(message)
)
// ...
case class Message(text: String)
class CometMessage extends CometActor {
override def lowPriority = {
case Message(text) => {
// do something here with the text, whether settings a SessionVar or even just a plain var
}
}
}
回答2:
From Lift's mailing list:
CometActors exist outside of the HTTP request/response cycle. This means that during the processing of a message in a CometActor, you don't get to see the Req because there's never a Req available as part of message processing.
Contrary to some of the posts on this thread, the S context is available as are SessionVars (but not RequestVars because the CometActor is outside of the scope of a request).
(...)
Your best bet is to create a SessionVar (or a few SessionVars) that hold the host and port. Set those SessionVars from a snippet (if they are not already set). Then access the SessionVars from your CometActors.
This is one possible option. However, my personal preference is to insert comets to page using NamedCometActorSnippet
and send a message to created comet immediately:
class MyCometSnippet extends NamedCometActorSnippet {
val cometClass = "MyComet"
val name = "MyComet-instance"
for {
liftSession <- S.session
processingMode <- S.param("message")
} yield {
liftSession.sendCometActorMessage(cometClass, Full(name), SetMessageValue(message))
}
}
来源:https://stackoverflow.com/questions/7915259/scala-lift-s-param-access-within-comet-actor