ajax 实现聊天室

很简单的一个聊天室程序,每一秒读一次(没办法),对比当前窗口与后台的对话作息数,得到需要返回到当前窗口的对话信息,不必每次都把所有的数据读一次。

自动滚屏,但是当人为滚动滚动条时,会停止自动滚屏。

主要的一个类:

 

 

  1 package  com.ttszw.action;
  2
  3 import  java.util.ArrayList;
  4 import  java.util.HashMap;
  5 import  java.util.List;
  6 import  java.util.Map;
  7 import  java.util.Random;
  8 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
  9 * coding utf-8
 10 * @author chengkai
 11 *
 12 */

 13 ExpandedBlockStart.gifContractedBlock.gif public   class  ChatMassage  {
 14    
 15    public static ChatMassage chatMassage = null;
 16    //用户
 17    private List<String> users = new ArrayList<String>();
 18    //最大聊天室人数
 19    private final int USER_NUMBER = 50;
 20    //聊天室人数
 21    public static int userNum=0;
 22    //对话信息
 23    private List<String> massages = new ArrayList<String>();
 24    //信息总数目
 25    public static int mass_num=0;
 26    //用户显示的颜色
 27    private Map<String, String> color = new HashMap<String, String>();
 28    
 29ExpandedSubBlockStart.gifContractedSubBlock.gif    protected ChatMassage(){}
 30    
 31ExpandedSubBlockStart.gifContractedSubBlock.gif    public static ChatMassage Instance(){
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        if(chatMassage==null){
 33            return chatMassage = new ChatMassage();
 34        }

 35        return chatMassage;
 36    }

 37    
 38ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 39     * 登记用户名
 40     * @param name 用户姓名
 41     * @return boolean 添加是否成功
 42     */

 43ExpandedSubBlockStart.gifContractedSubBlock.gif    public boolean addUser(String name){
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        synchronized (users) {
 45            //如果没有此用户,而且聊天室人数未满
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            if(!this.hasUser(name)&&!isFull()){
 47                users.add(name);
 48                //为当前用户设置显示颜色
 49                String color = createColor();
 50                this.color.put(name, color);
 51                userNum++;
 52                return true;
 53            }

 54        }

 55        return false;
 56    }

 57    
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    public boolean isFull(){
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        if(userNum<this.USER_NUMBER){
 60            return false;
 61        }

 62        return true;
 63    }

 64ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 65     * 查看此用户名是否被使用
 66     * @param name 用户姓名
 67     * @return boolean 是否存在
 68     */

 69ExpandedSubBlockStart.gifContractedSubBlock.gif    public boolean hasUser(String name){
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        for(String na:users){
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            if(name.equalsIgnoreCase(na)){
 72                return true;
 73            }

 74        }

 75        return false;
 76    }

 77ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 78     * 得到用户列表
 79     * @return
 80     */

 81ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getUsers(){
 82        StringBuffer user = new StringBuffer();
 83        int i=0;
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        while(i<userNum){
 85            String name = this.users.get(i);
 86            String color = this.getColor(name);
 87            String userMass = "<font color='"+color+"'>"+name+"</font><br>";
 88            user.append(userMass);
 89            i++;
 90        }

 91        return user.toString();
 92    }

 93ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 94     * 返回信息
 95     * @param i 前台中存有的对话信息数
 96     * @return
 97     */

 98ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getMassage(int i){
 99        StringBuffer massage = new StringBuffer();
100        //只有在前台对话信息数小于后台信息总数时才返回
101ExpandedSubBlockStart.gifContractedSubBlock.gif        while(i<mass_num&&i>=0){
102            massage.append(massages.get(i));
103            i++;
104        }

105        return massage.toString();
106    }

107    
108ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
109     * 添加对话信息
110     * @param massage 信息内容
111     */

112ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setMassage(String massage){
113ExpandedSubBlockStart.gifContractedSubBlock.gif        synchronized (massages) {
114            massages.add(massage);
115            mass_num++;
116        }

117    }

118    
119ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
120     * 退出聊天室
121     * @param name 退出人的姓名
122     */

123ExpandedSubBlockStart.gifContractedSubBlock.gif    public void exit(String name){
124ExpandedSubBlockStart.gifContractedSubBlock.gif        if(users.remove(name)){
125            userNum--;
126            //如果聊天室没有人了,则始化信息
127ExpandedSubBlockStart.gifContractedSubBlock.gif            if(userNum==0){
128                mass_num=0;
129                massages.clear();
130                users.clear();
131                color.clear();
132ExpandedSubBlockStart.gifContractedSubBlock.gif            }
else{
133                color.remove(name);
134            }

135        }
;
136    }

137ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
138     * 根据用户名得到当前用户的颜色
139     * @param username
140     * @return
141     */

142ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getColor(String username) {
143        return color.get(username);
144    }

145ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
146     * 随机创建颜色
147     * @return
148     */

149ExpandedSubBlockStart.gifContractedSubBlock.gif    private String createColor(){
150        StringBuffer color=new StringBuffer("#");
151        Random random = new Random();
152ExpandedSubBlockStart.gifContractedSubBlock.gif        for(int i=0;i<3;i++){
153            int a = random.nextInt(256);
154            //转换成十六进制
155            color.append(Integer.toHexString(a));
156        }

157        return color.toString();
158    }

159    
160}

 程序如下:

/Files/ksyou/ChatOnlineDemo.rar

由于使用了json所以还要加一些jar

/Files/ksyou/allJarOfJson.rar

转载于:https://www.cnblogs.com/ksyou/archive/2009/04/30/1447201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值