SCALA Lift - S.param access within Comet Actor

前端 未结 2 1169
走了就别回头了
走了就别回头了 2020-12-17 05:13

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, whic

2条回答
  •  有刺的猬
    2020-12-17 06:20

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

提交回复
热议问题