【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了ChatRoom.java提供大家参考,希望对大家有所帮助!
package chatroom;
import java.util.*;
/**
* Title: JSP聊天室
* Description: Java语言演示程序:JSP聊天室,用于北京师范大学计算机系Java课程教学示范。
* Copyright: Copyright (c) 2002
* Company: 北京师范大学计算中心
* @author 张庆利
* @version 1.0
*/
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,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);
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){
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){
return theUserList[chatroom][i].getUserId();
}
public String returnUserName(int chatroom,int i){
return theUserList[chatroom][i].getUserName();
}
public String returnUserName(int chatroom,String userId){
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;
}
}
上一篇:
chatreg.java
下一篇:
多年来只想说一句,我不怪你