jhdevuk jhdevuk - 6 months ago 16
Ajax Question

SCALA Lift - S.param access within Comet Actor

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 :)

Answer

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
    }
  }
}
Comments