1 package org.getahead.dwrdemo.chat; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 import java.util.LinkedList ; 6 7 import org.directwebremoting.ScriptBuffer; 8 import org.directwebremoting.ScriptSession; 9 import org.directwebremoting.WebContext; 10 import org.directwebremoting.WebContextFactory; 11 import org.directwebremoting.util.Logger; 12 13 16 public class JavascriptChat 17 { 18 21 public void addMessage(String text) 22 { 23 if (text != null && text.trim().length() > 0) 24 { 25 messages.addFirst(new Message(text)); 26 while (messages.size() > 10) 27 { 28 messages.removeLast(); 29 } 30 } 31 32 WebContext wctx = WebContextFactory.get(); 33 String currentPage = wctx.getCurrentPage(); 34 35 ScriptBuffer script = new ScriptBuffer(); 36 script.appendScript("receiveMessages(") 37 .appendData(messages) 38 .appendScript(");"); 39 40 Collection pages = wctx.getScriptSessionsByPage(currentPage); 42 for (Iterator it = pages.iterator(); it.hasNext();) 43 { 44 ScriptSession otherSession = (ScriptSession) it.next(); 45 otherSession.addScript(script); 46 } 47 } 48 49 52 private LinkedList messages = new LinkedList (); 53 54 57 protected static final Logger log = Logger.getLogger(JavascriptChat.class); 58 } 59 | Popular Tags |