sendmessage.java

来自「开始学习dwr 聊天程序使用dwr的推模式」· Java 代码 · 共 60 行

JAVA
60
字号
package com.lhq;
/** 
 * @author 邓健 dengjian1985@163.com 
 * @version 创建时间:Mar 18, 2009 4:18:49 PM 
 * 类说明 
 */
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;

public class SendMessage {
 public void addMessage(String message)
 {
String s = "我对你说:"+message;
  messages.add(s);
System.out.println(s);
  
//	 WebContext wctx = WebContextFactory.get();
//     String currentPage = wctx.getCurrentPage();
//
//     ScriptBuffer script = new ScriptBuffer();
//     script.appendScript("receiveMessages(")
//           .appendData(messages)
//           .appendScript(");");
//
//     // Loop over all the users on the current page
//     Collection pages = wctx.getScriptSessionsByPage(currentPage);
//     for (Iterator it = pages.iterator(); it.hasNext();)
//     {
//         ScriptSession otherSession = (ScriptSession) it.next();
//         otherSession.addScript(script);
//     }
//  
  WebContext wctx = WebContextFactory.get();
  String currentPage = wctx.getCurrentPage();

  // Clear the input box in the browser that kicked off this page only
  Util utilThis = new Util(wctx.getScriptSession());
  utilThis.setValue("text", "");

  // For all the browsers on the current page:
  Collection sessions = wctx.getScriptSessionsByPage(currentPage);
  Util utilAll = new Util(sessions);

  // Clear the list and add in the new set of messages
  utilAll.removeAllOptions("chatlog");
  utilAll.addOptions("chatlog", messages, "text");
  
 }
 private List<String> messages=new LinkedList<String>();
 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?