ChatRoom.java:
package customers;
import java.util.*;
public class ChatRoom
{speaking chatSpeaking[][];/*聊天室的全部发言内容*/
private int speakingCount[];/*聊天室的发言条数*/
private int userCount[];/*聊天室的在线人数*/
oneUser theUserList[][];/*聊天室的在线用户列表*/
public ChatRoom()
//构造函数
{theUserList=new oneUser[3][100];
userCount=new int[3];
chatSpeaking=new speaking[3][50];
speakingCount=new int[3];
userCount[1]=0;
userCount[2]=0;
speakingCount[1]=0;
speakingCount[2]=0;
}
public void addUserList(String theOneUserId,String theOneUserName,
long theOneUserTime,String theOneUserPosion,String theOneUserSwjg,
String theOneUserSfks,int chatroom)
//向在线用户列表中添加用户,如果用户已存在,将不重复添加
{int i;
for (i=1;i<=userCount[chatroom];i++) if (returnUserId(chatroom,i)
.equals(theOneUserId)) break;
if (i==userCount[chatroom]+1)
{userCount[chatroom]=userCount[chatroom]+1;
oneUser theOneUser=new oneUser();
theOneUser.setOneUser(theOneUserId,theOneUserName,theOneUserTime,
theOneUserPosion,theOneUserSwjg,theOneUserSfks);
theUserList[chatroom][userCount[chatroom]]=theOneUser;
}
}
public void deleteUserList(String theOneUserId,int chatroom)
//从在线用户列表中删除用户
{int i;
for (i=1;i<=userCount[chatroom];i++) if (returnUserId(chatroom,i)
.equals(theOneUserId))
theUserList[chatroom][i]=theUserList[chatroom][userCount[chatroom]];
userCount[chatroom]=userCount[chatroom]-1;
}
public void addSpeaking(String theSpeaking,String theSourceUserId,
String theTargetUserId,String theSourceUserName,String theTargetUserName,
String theFace,String theColor,String theRadprivate,int chatroom)
//增加聊天信息,如果发言条数未超过30条,则添加一条,发言条数相应加1;
反之,将丢弃最先的一条发言,再将新发言添加到最后,发言条数不变
{speaking thisSpeaking=new speaking();
thisSpeaking.setSpeaking(theSpeaking,theSourceUserId,theTargetUserId,
theSourceUserName,theTargetUserName,theFace,theColor,theRadprivate);
if (speakingCount[chatroom]<=30)
{speakingCount[chatroom]=speakingCount[chatroom]+1;
chatSpeaking[chatroom][speakingCount[chatroom]]=thisSpeaking;
}
else
{int i;
for (i=1;i<speakingCount[chatroom];i++) chatSpeaking[chatroom][i]
chatSpeaking[chatroom][i+1];
chatSpeaking[chatroom][speakingCount[chatroom]]=thisSpeaking;
}
}
public void setUserTime(String theUserId,long theUserTime,int intchatroom)
//设置用户时间
{int i;
for (i=1;i<=userCount[intchatroom];i++) if ((theUserList[intchatroom][i]
.getUserId()).equals(theUserId))
{theUserList[intchatroom][i].setUserTime(theUserTime);
break;
}
}
public int returnUserCount(int chatroom)
//获取在线人数
{return userCount[chatroom];
}
public int returnSpeakingCount(int chatroom)
//获取发言条数
{return speakingCount[chatroom];
}
public String returnUserId(int chatroom,int i)
//获取聊天室中指定序号的用户的用户ID
{return theUserList[chatroom][i].getUserId();
}
public String returnUserName(int chatroom,int i)
//获取聊天室中指定序号的用户的用户名
{return theUserList[chatroom][i].getUserName();
}
public String returnUserName(int chatroom,String userId)
//获取聊天室中指定用户ID的用户的用户名
{String UserName=new String("");
int i;
for (i=1;i<=userCount[chatroom];i++)
if (returnUserId(chatroom,i).equals(userId))
UserName=theUserList[chatroom][i].getUserName();
return UserName;
}
public String returnAllSpeaking(int chatroom,String thisUser)
//获取某用户可见的全部聊天信息
{int i;
java.util.Date dateTimenow=new java.util.Date();
long longDateTimeNow=(long)dateTimenow.getTime();
setUserTime(thisUser,longDateTimeNow,chatroom);
for (i=1;i<=userCount[chatroom];i++)
{long oneUserTime=(long)theUserList[chatroom][i].getUserTime();
if (longDateTimeNow - oneUserTime>60000)
{addSpeaking("","","",theUserList[chatroom][i].getUserName(),"","","green",
"system",chatroom);
deleteUserList(theUserList[chatroom][i].getUserId(),chatroom);
//在调用该方法时,同时判断有无超时用户,如有,将其从在线用户列
表中自动删除,并发出相应系统公告
}
}
String allSpeaking=new String("");
for (i=1;i<=speakingCount[chatroom];i++)
allSpeaking=allSpeaking+chatSpeaking[chatroom][i]
.returnSpeaking(thisUser);
return allSpeaking;
}
}
如果您对本文有任何疑问或者建议,请到讨论区发表您的意见:
>>
论坛入口 <<
上一页12 3 4 5 6 下一页
上一篇:JSP专题:第六部分:用JSP实现聊天室-1
下一篇:不同Web主机上的Servlet之间数据对象的相互传输
|