SCALA Lift - S.param access within Comet Actor

前端 未结 2 1170
走了就别回头了
走了就别回头了 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 05:55

    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))
      }
    }
    
    0 讨论(0)
  • 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
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题