Re: [Lift] Comet chat stop working after a random time delay

27 views
Skip to first unread message

Matt Farmer

unread,
May 16, 2015, 9:23:28 PM5/16/15
to Lift
Hi,

Can you set up a sample project on GitHub that I can clone and play with? This does seem curious… I’d like to take a peek at it and see if I can figure out what’s going on.

Thanks,


Matt Farmer Blog | Twitter

On May 15, 2015, at 9:41 AM, Mirko Saiu <mirko...@gmail.com> wrote:

We have this kind of incomprehensible problem and we even don't know how exactly reproduce it. We just stop using the comet chat for a while keeping the web browser page open, then when we try to use the chat again , it doesn't works. It will works again only if you logout and login.

Jetty version is 8.1.17
Lift version is 2.6

Can someone help us?

Here some code..

The chat snippet:

class chatin extends Loggable {
  var message = ""
  var idExperience = S.param("key").map(_.toString) openOr("nudda")
  var meeting: Meeting = _
  var usr: User = _


  User.currentUser.map{ x =>
    usr = x
    meeting = Meeting.findByStringIdActive(idExperience).getOrElse{S.redirectTo("/404")}
    if (!meeting.belongsToUser(usr.id.toString())) S.redirectTo("/404")

    GMRConfig.sessionVars.last_meetingExp.set(idExperience)
    usr.last_meeting(idExperience).save


  } getOrElse({logger.error("Meeting, no user is logged")})



  def doSubmit() : JsCmd = {

    println("CHAT dosubmit 1")

    User.currentUser.map({ usr =>
      Meeting.findByStringId(idExperience).map({
        println("CHAT dosubmit 2 message: " + message)
        mp => mp.addChatMessage(usr, message)
      })
      SetValById("chat_in", "") & // clear the input box
        Call("scrollChat").cmd
    }) openOr {logger.error("user not logged in") }
  }

  def bindchat(xhtml: NodeSeq) = {

    var chatlist = <ul id="main_ul_id">
      <li id="main_li_id">
        <div name="fumetto">
          <img name="img" class="triang"/>
          <div name="body">
            {ChatMessage.where(_.meeting eqs GMRConfig.sessionVars.last_meetingExp.get).orderDesc(_.time).fetch.foreach(x=> line(x))}
          </div>
        </div>
        <div class="clear-both"></div>
      </li>
    </ul>

    var html =
      <div id="divChat" class="chat-box box" data-lift={"comet?type=Chat;name=" + idExperience + ";ul_id=main_ul_id;li_id=main_li_id"}>
        <div class="message-list">
          {chatlist}
        </div>

        <div class="form-input">
          <form class="lift:form.ajax">
            {SHtml.textarea("", message = _, "id" -> "chat_in")}
            {SHtml.ajaxSubmit(S.?("send"), doSubmit, "id" -> "send")}
          </form>
        </div>
      </div>


    bind("print", xhtml, "chat" -> html)
  }


  def line(c: ChatMessage) = {
    if (c.userEmail.get != User.currentUser.get.email.get){
      ("name=fumetto [class]" #> "fumetto-destinatario pull-left clear-both" &
        "name=img [src]" #> "/img/triang_conv_sx.png" &
        "name=body" #> c.message.get)
    }
    else{
      ( "name=fumetto [class]" #> "fumetto-mittente pull-right clear-both" &
        "name=img [src]" #> "/img/triang_conv_dx.png" &
        "name=body" #> c.message.get )
    }
  }


}

The CometActor and the LiftActor:


class Chat extends CometActor with CometListener with Loggable{

  private lazy val li = liId.flatMap(Helpers.findId(defaultHtml, _)) openOr NodeSeq.Empty
  private val ulId = S.attr("ul_id") openOr "some_ul_id"
  private val liId = S.attr("li_id")
  var meetingID = S.param("key").getOrElse("nudda")
  var chats: List[ChatMessage] =  ChatMessage.where(_.meeting eqs meetingID).orderDesc(_.time).fetch

  def updateChats(list: List[ChatMessage]) = {
      chats = list
  }


  override def exceptionHandler = {
    case e => logger.error(e.getStackTraceString)
  }


  override def lowPriority = {
    
    case x: ChatMessage => if(meetingID == x.meeting.get && !chats.contains(x)) {
      println("CHAT lowPriority: " + x.message.get)
      chats = x::chats
      val update = CmdPair(AppendHtml(ulId, line(x)),Call("scrollChat").cmd)
      partialUpdate(update)
    }
  }

  // register this component
  def registerWith = ChatServer

  // render the whole list of chats
  override def render = {
    ("#" + ulId + " *") #> {displayList}
  }

  // display a list of chats
  private def displayList: NodeSeq = chats.reverse.flatMap(line)

  // display a line
  private def line(c: ChatMessage) = {

      ("name=fumetto [class]" #> "fumetto-destinatario  pull-left clear-both" &
        "name=li [class]" #> "fumetto-sinistra  pull-right clear-both" &
        "name=img [src]" #> "/img/triang_conv_sx.png" &
        "name=body" #> c.message.is)(li)}
      else{

      ( "name=fumetto [class]" #> "fumetto-mittente  pull-right clear-both" &
        "name=li [class]" #> "fumetto-destra  pull-right clear-both" &
        "name=img [src]" #> "/img/triang_conv_dx.png" &
        "name=body" #> c.message.is)(li)
    }


  }


}


object ChatServer extends LiftActor with ListenerManager with Loggable{

  //variablie che viene aggiornata aad ogni meering page aperta
 var chat: ChatMessage = _

  override protected def exceptionHandler = {
    case e => logger.error(e.getStackTraceString)
  }

  override def lowPriority = {


    case v: ChatMessage  =>
      chat = v
      updateListeners()

    case _ =>
  }

  def createUpdate = chat
  def toHtml(msg: String): NodeSeq = <div>{msg}</div>

}





--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mirko Saiu

unread,
May 20, 2015, 5:40:00 AM5/20/15
to lif...@googlegroups.com
Ok, we tried any kind of solution, from updating jetty and lift framework to changing the server memory configuration and go on.. at the end the solution was very much simpler. 

We had the html annidated like this:

<div data-lift="Snippet1">
   
   
<div data-lift="ChatSnippet"></div>

</div>

We resolved doing this:

<div data-lift="ChatSnippet"></div>

<div data-lift="Snippet1"></div>


Very simple
Reply all
Reply to author
Forward
0 new messages