Scala Lift: Multiple Comet Actors on same page
Is it a good idea to have more than one comet actor on a page?
I've currently got a simple chat box like the one in the Lift docs and a simple webcam stream handler.
I'm experiencing locks when both are activated, e.g I type a message into the chat, all is well, but when I start a camera feed the chat box no longer works.
What is best practice for this kind of setup? I aim to have alot more than two on a page, but was wondering if it would be better to have a single actor that dispatches the results to the page for both chat and streams etc.
Code is posted below:
CHAT
case class ChatMessage(name: String, text: String)
class ChatComet extends CometActor with CometListener {
private var messages: List[ChatMessage] = Nil
def registerWith = ChatServer
override def lowPriority = {
case v: List[ChatMessage] => messages = v; reRender()
}
def render = {
"li *" #> messages.map(message =>
".name *" #> message.name &
".text *" #> message.text
开发者_如何转开发 )
}
}
object ChatServer extends LiftActor with ListenerManager {
private var messages: List[ChatMessage] = Nil
def createUpdate = messages
override def lowPriority = {
case message: String if message.length > 0 =>
messages :+= ChatMessage("Anon", message);
updateListeners()
}
}
STREAM (Webcam)
case class StreamItem(name: String, path: String, level: String)
class StreamComet extends CometActor with CometListener {
private var streams: List[StreamItem] = Nil
def registerWith = StreamServer
override def lowPriority = {
case v: List[StreamItem] =>
streams = v;
reRender();
partialUpdate(Call("STREAMMOD.stream_view.add_stream({ path : '" + streams.reverse(0).path + "', level : '_1'})"));
}
def render = {
"li *" #> streams.map(stream =>
".name *" #> stream.name.toString &
".stream [id]" #> stream.path.toString
)
}
}
object StreamServer extends LiftActor with ListenerManager {
private var streams: List[StreamItem] = Nil
def createUpdate = streams
override def lowPriority = {
case stream: String if stream.length > 0 =>
streams :+= StreamItem("James", stream, "_1");
updateListeners()
}
}
Several comet actors per page should work fine. The most I've had on one page is 11. In other projects I've had 4. Some might think it is overkill, but it lets me have smaller actors with limited responsibility. Lift handles all the plumbing of getting the actors to run through one connection to the server which means that it won't use more resources than needed.
The code look right, but I would have investigated STREAMMOD.stream_view.add_stream
.
I know that this answer comes late, but it might help others.
精彩评论