当前位置: 技术问答>java相关
用java写聊天室
来源: 互联网 发布时间:2017-04-15
本文导语: 如何把每个客户端写入的信息在服务端接收并写回给每个客户端,每个客户端显示的都是所有客户端的信息 | Socket连接:Applet--Server 多线程(每一个Socket连接用一个线程来管理) 要注意同步 ...
如何把每个客户端写入的信息在服务端接收并写回给每个客户端,每个客户端显示的都是所有客户端的信息
|
Socket连接:Applet--Server
多线程(每一个Socket连接用一个线程来管理)
要注意同步
多线程(每一个Socket连接用一个线程来管理)
要注意同步
|
用RMI
//这是我曾经做过的一个分析,不完整,仅供参考
1. 注册用户RegisteredUser:在系统中注册的用户
2. 登录用户LoginedUser:登录到系统中的用户
3. 聊天室Chatroom:聊天室
4. 聊天Talk:登录用户在某个聊天室和某个用户聊天
5. 聊天内容Message:聊天的内容(聊天内容可以分为
一对一的聊天,某个人对所有人说的话,系统发出
的通告等)
LoginedUser和RegisteredUser是0。。1对1关系(单向关联)。
Chatroom和Message是1..n关系(聚合,单向)。
LoginedUser和Chatroom之间的关系比较复杂,如果不包含一个LoginedUser
和另一LoginedUser在一个Chatroom聊天的事实,那么LoginedUser和Chatroom是
0。。n对0。。n的关系(双向关联)。加上聊天这个关系后,就成了三元关系,
可以用关联类Talk来表示。
class RegisteredUser extends Persistent {
private String id;
private String password;
private String email;
public RegisteredUser() {} //for Serialization
public RegisteredUser(String id) {} //for register
public static RegisteredUser getUser(String id,String password){ //工厂方法
//从文件中去取
//取了之后new出对象。
如果找不到id的注册对象,则返回null。
}
public static boolean isRegistered(String id) {}
public static Iterator iterator() {}
public void delete() {}
public void store() {}
public void update() {}
public String getID() {}
public String getPassword(){}
public String getEmail() {}
public changePassword(String newPassword) {}
public changeEmail(String newEmail) {}
public void read(RamdomAccessFile file) {}
public void write() {RamdomAccessFile file{}
}
class LoginedUser {
static Vector users = new Vector(100);
private RegisteredUser ru;
Vector rooms = new Vector(10); //Object is Talk ,关联名当参数
LoginedUser(RegisterdUser ru) {
this.ru = ru;
}
public static LoginedUser addUser(String name,String password) {
如果失败,返回null。只有注册过的用户采用add。
}
public static void removeUser(LoginedUser user) {
}
public RegisteredUser getRegiseredUser() { return ru; }
public String getID() { return ru.getID(); }
public Talk enterRoom(Chatroom room) { //核心方法,处理和Chatroom之间的关联,并new出Talk对象
//是Talk对象的工厂方法
Talk er = new Talk(room,this);
users.add(er);
room.enter(er);
return Talk;
}
public leaveRoom(Chatroom room) { //核心方法
Talk er = getRoom();
rooms.remove(er);
room.leave(er)
}
public Chatroom[] getRooms() {
返回数组,只读
}
private Talk getTalk(Chatroom room) {
//search talk with chatroom
}
}
//
class Chatroom { //消息容器,注意不是消息Manager,因为Chatroom不负责消息的创建
private static Vector rooms = new Vector(10); //all room in system
private String name;
private Vector users = new Vector(10);//用关联名当域名
private Vector messages = new Vectors(100);
public static ChatRoom createRoom(String name) {} //Room的工厂方法
public static ChatRoom[] getAllRoom() {}
public enter(EnterRoom u) {
users.add(u);
}
public void leave(EnterRoom u) {
user.remove(u);
}
public LoginedUsers[] getAllUsers() {
//返回数组,用于只读
}
public void send(Message message) {
}
}
class Talk { //双向关联类或者应该是三向关联类
Chatroom room;
LoginedUser source; //说话者
LoginedUser dest; //听话者
long time; //create time of talk,it is time when enter the chat room
public EnterRoom(ChatRoom room,LoginedUser,LoginedUser user) {
assert(room!=null);
assert(user!=null);
}
public Chatroom getRoom() {}
public LoginedUser getUser() {}
public void getTime() {}
public void speak(String message) {
Message msg = new Message();
Message.setSource(source);
Message.setDest(dest);
Message.setTime(currentTime);
room.send(Message msg);
}
}
class Message {
LoginedUser source;
LoginedUser dest;
long time;
public Message() {}
public void setSource(LoginedUser )
public void setDest(LoginedUser )
public void setTime(long);
public boolean isFromSystem() {
}
public boolean isBroadcast(){
}
public LoginedUser getSource();
public LoginedUser getDest();
}
{
注册过程
read id from GUI;
if RegisteredUser.isRegisted(id) {
messagebox();
reuturn;
}
RegisteredUser r = new RegisteredUser(id);
r.changePassword();
r.changeEmail();
r.store();
}
{
登录过程
read in id and password from GUI;
if ((user = LoginedUser.addUser(id,password)) == null) {
messagebox();
return;
}
LoginedUser user = LoginedUser.get(id);
RegisteredUser ru = user.getRegistedUser();
}
{
修改资料过程
LoginedUser user = LoginedUser.get(id);
RegisteredUser ru = user.getRegistedUser();
ru.setEmail();
ru.store();
}
{
聊天过程
LoginedUser user = loginedUser.get(id);
Talk talk = user.enterRoom(aroom);
talk.selectPeer();
talk.speak();
talk.speak();
talk.speak();
}
//这是我曾经做过的一个分析,不完整,仅供参考
1. 注册用户RegisteredUser:在系统中注册的用户
2. 登录用户LoginedUser:登录到系统中的用户
3. 聊天室Chatroom:聊天室
4. 聊天Talk:登录用户在某个聊天室和某个用户聊天
5. 聊天内容Message:聊天的内容(聊天内容可以分为
一对一的聊天,某个人对所有人说的话,系统发出
的通告等)
LoginedUser和RegisteredUser是0。。1对1关系(单向关联)。
Chatroom和Message是1..n关系(聚合,单向)。
LoginedUser和Chatroom之间的关系比较复杂,如果不包含一个LoginedUser
和另一LoginedUser在一个Chatroom聊天的事实,那么LoginedUser和Chatroom是
0。。n对0。。n的关系(双向关联)。加上聊天这个关系后,就成了三元关系,
可以用关联类Talk来表示。
class RegisteredUser extends Persistent {
private String id;
private String password;
private String email;
public RegisteredUser() {} //for Serialization
public RegisteredUser(String id) {} //for register
public static RegisteredUser getUser(String id,String password){ //工厂方法
//从文件中去取
//取了之后new出对象。
如果找不到id的注册对象,则返回null。
}
public static boolean isRegistered(String id) {}
public static Iterator iterator() {}
public void delete() {}
public void store() {}
public void update() {}
public String getID() {}
public String getPassword(){}
public String getEmail() {}
public changePassword(String newPassword) {}
public changeEmail(String newEmail) {}
public void read(RamdomAccessFile file) {}
public void write() {RamdomAccessFile file{}
}
class LoginedUser {
static Vector users = new Vector(100);
private RegisteredUser ru;
Vector rooms = new Vector(10); //Object is Talk ,关联名当参数
LoginedUser(RegisterdUser ru) {
this.ru = ru;
}
public static LoginedUser addUser(String name,String password) {
如果失败,返回null。只有注册过的用户采用add。
}
public static void removeUser(LoginedUser user) {
}
public RegisteredUser getRegiseredUser() { return ru; }
public String getID() { return ru.getID(); }
public Talk enterRoom(Chatroom room) { //核心方法,处理和Chatroom之间的关联,并new出Talk对象
//是Talk对象的工厂方法
Talk er = new Talk(room,this);
users.add(er);
room.enter(er);
return Talk;
}
public leaveRoom(Chatroom room) { //核心方法
Talk er = getRoom();
rooms.remove(er);
room.leave(er)
}
public Chatroom[] getRooms() {
返回数组,只读
}
private Talk getTalk(Chatroom room) {
//search talk with chatroom
}
}
//
class Chatroom { //消息容器,注意不是消息Manager,因为Chatroom不负责消息的创建
private static Vector rooms = new Vector(10); //all room in system
private String name;
private Vector users = new Vector(10);//用关联名当域名
private Vector messages = new Vectors(100);
public static ChatRoom createRoom(String name) {} //Room的工厂方法
public static ChatRoom[] getAllRoom() {}
public enter(EnterRoom u) {
users.add(u);
}
public void leave(EnterRoom u) {
user.remove(u);
}
public LoginedUsers[] getAllUsers() {
//返回数组,用于只读
}
public void send(Message message) {
}
}
class Talk { //双向关联类或者应该是三向关联类
Chatroom room;
LoginedUser source; //说话者
LoginedUser dest; //听话者
long time; //create time of talk,it is time when enter the chat room
public EnterRoom(ChatRoom room,LoginedUser,LoginedUser user) {
assert(room!=null);
assert(user!=null);
}
public Chatroom getRoom() {}
public LoginedUser getUser() {}
public void getTime() {}
public void speak(String message) {
Message msg = new Message();
Message.setSource(source);
Message.setDest(dest);
Message.setTime(currentTime);
room.send(Message msg);
}
}
class Message {
LoginedUser source;
LoginedUser dest;
long time;
public Message() {}
public void setSource(LoginedUser )
public void setDest(LoginedUser )
public void setTime(long);
public boolean isFromSystem() {
}
public boolean isBroadcast(){
}
public LoginedUser getSource();
public LoginedUser getDest();
}
{
注册过程
read id from GUI;
if RegisteredUser.isRegisted(id) {
messagebox();
reuturn;
}
RegisteredUser r = new RegisteredUser(id);
r.changePassword();
r.changeEmail();
r.store();
}
{
登录过程
read in id and password from GUI;
if ((user = LoginedUser.addUser(id,password)) == null) {
messagebox();
return;
}
LoginedUser user = LoginedUser.get(id);
RegisteredUser ru = user.getRegistedUser();
}
{
修改资料过程
LoginedUser user = LoginedUser.get(id);
RegisteredUser ru = user.getRegistedUser();
ru.setEmail();
ru.store();
}
{
聊天过程
LoginedUser user = loginedUser.get(id);
Talk talk = user.enterRoom(aroom);
talk.selectPeer();
talk.speak();
talk.speak();
talk.speak();
}