关于聊天室中工具类部分
整体思路
![image-20220616214713744]()
把要传输的内容封装成了两个类 Response 和 Request,客户端向服务器发起请求,服务器向客户端回应,通过两个类中包含的请求类型来判断需要进行的操作,传输采用ObjectStream。仔细以看其实会发现,这两个类内容很相似。
Request
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| public class Request implements Serializable {
private static final long serialVersionUID = -1237018286305074249L;
//请求传送的数据类型
private ResponseType type;
//请求动作
private String action;
//请求域中的数据,key-value
private Map<String, Object> attributesMap;
public Request(){
this.attributesMap = new HashMap<String, Object>();
}
public ResponseType getType() {
return type;
}
public void setType(ResponseType type) {
this.type = type;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public Map<String, Object> getAttributesMap() {
return attributesMap;
}
public Object getAttribute(String name){
return this.attributesMap.get(name);
}
public void setAttribute(String name, Object value){
this.attributesMap.put(name, value);
}
public void removeAttribute(String name){
this.attributesMap.remove(name);
}
public void clearAttribute(){
this.attributesMap.clear();
}
}
|
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| public class Response implements Serializable {
private static final long serialVersionUID = 1689541820872288991L;
//响应状态
private ResponseStatus status;
//响应数据类型
private ResponseType type;
//响应中的数据,key-value
private Map<String,Object> dataMap;
//响应输出流
private OutputStream outputStream;
public Response(){
this.status = ResponseStatus.OK;
this.dataMap = new HashMap<String, Object>();
}
public ResponseStatus getStatus() {
return status;
}
public void setStatus(ResponseStatus status) {
this.status = status;
}
public ResponseType getType() {
return type;
}
public void setType(ResponseType type) {
this.type = type;
}
public Map<String, Object> getDataMap() {
return dataMap;
}
public void setDataMap(Map<String, Object> dataMap) {
this.dataMap = dataMap;
}
public OutputStream getOutputStream() {
return outputStream;
}
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
public void setData(String name, Object value){
this.dataMap.put(name, value);
}
public Object getData(String name){
return this.dataMap.get(name);
}
public void removeData(String name){
this.dataMap.remove(name);
}
public void clearData(){
this.dataMap.clear();
}
}
|
在以上两个类中,传输的内容会包括文件和消息,对于文件和消息,我们需要直到发送者和接受者是谁,需要知道发送时间等等,所以同样封装成了两个类。
FileInfo
用于传送和接收文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| //传送文件的工具类
public class FileInfo implements Serializable {
private static final long serialVersionUID = -5394575332459969403L;
//文件接收者
private User toUser;
//文件发送者
private User fromUser;
//源文件名
private String srcName;
//发送时间
private Date sendTime;
//目标地IP
private String destIp;
//目标地端口
private int destPort;
//目标文件名
private String destName;
public User getToUser() {
return toUser;
}
public void setToUser(User toUser) {
this.toUser = toUser;
}
public User getFromUser() {
return fromUser;
}
public void setFromUser(User fromUser) {
this.fromUser = fromUser;
}
public String getSrcName() {
return srcName;
}
public void setSrcName(String srcName) {
this.srcName = srcName;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
public String getDestIp() {
return destIp;
}
public void setDestIp(String destIp) {
this.destIp = destIp;
}
public int getDestPort() {
return destPort;
}
public void setDestPort(int destPort) {
this.destPort = destPort;
}
public String getDestName() {
return destName;
}
public void setDestName(String destName) {
this.destName = destName;
}
}
|
Message
用于发送和接收消息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| public class Message implements Serializable{
private static final long serialVersionUID = 1820192075144114657L;
//消息接收者
private User toUser;
//消息发送者
private User fromUser;
//消息内容
private String message;
//消息发送时间
private Date sendTime;
public User getToUser() {
return toUser;
}
public void setToUser(User toUser) {
this.toUser = toUser;
}
public User getFromUser() {
return fromUser;
}
public void setFromUser(User fromUser) {
this.fromUser = fromUser;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
}
|
User
User 类则用于存储用户信息,因为会用于传输,需实现序列化传输。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
| public class User implements Serializable{
private static final long serialVersionUID = 5942011574971970871L;
//账号
private long id;
//passwd
private String password;
//昵称
private String nickname;
//头像
private int head;
//性别
private char sex;
public User(String password,String nickname,char sex,int head){
this.password=password;
this.sex=sex;
this.head=head;
if(nickname.equals("")||nickname==null){
this.nickname = "未命名";
}else{
this.nickname=nickname;
}
}
public User(long id,String password){
this.id=id;
this.password=password;
}
public long getId(){
return id;
}
public void setId(long id){
this.id=id;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return this.password;
}
public void setSex(char sex){
this.sex=sex;
}
public char getSex(){
return this.sex;
}
public void setNickname(String nickname){
this.nickname=nickname;
}
public String getNickname(){
return this.nickname;
}
public void setHead(){
this.head=head;
}
public int getHead(){
return this.head;
}
//获取头像文件
public ImageIcon getHeadIcon(){
ImageIcon image = new ImageIcon("images/"+head+".png");
return image;
}
//生成hash码
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + head;
result = prime * result + (int)(id ^ (id >> 32));
result = prime * result + ((nickname == null) ? 0 : nickname.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + sex;
return result;
}
//重写对象比较函数 当且仅当两个对象的头像 账号 性别 昵称 密码 全都相同时 返回true
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
User other = (User) obj;
if(head != other.head || id != other.id || sex != other.sex)
return false;
if(nickname == null){
if(other.nickname != null)
return false;
}else if(!nickname.equals(other.nickname))
return false;
if(password == null){
if(other.password != null)
return false;
}else if(!password.equals(other.password))
return false;
return true;
}
//转换为string
@Override
public String toString() {
return this.getClass().getName()
+ "[id=" + this.id
+ ",pwd=" + this.password
+ ",nickname=" + this.nickname
+ ",head=" + this.head
+ ",sex=" + this.sex
+ "]";
}
}
|
ResponseStatus
1
2
3
4
5
6
7
8
9
10
11
| //响应状态枚举
public enum ResponseStatus {
OK, //请求处理成功
SERVER_ERROR, //服务器内部出错
NOT_FOUND, //请求的资源未找到
BAD_REQUEST //错误的请求对象
}
|
ResponseType
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| //响应数据的类型枚举
public enum ResponseType {
TEXT, //文本内容
TOSENDFILE, //准备发送文件
AGREERECEIVEFILE, //同意接收文件
REFUSERECEIVEFILE, //拒绝接收文件
RECEIVEFILE, //发送文件
LOGIN, //用户登录
LOGOUT, //用户退出
CHAT, //聊天
OTHER, //其他
BOARD, //服务器广播
REMOVE //服务器剔除用户
}
|
IOUtil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| public class IOUtil {
//关闭字节输入流
public static void close(InputStream is){
if(is != null){
try{
is.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
//关闭字节输出流
public static void close(OutputStream os){
if(os != null){
try{
os.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
//关闭字节输入流和输出流
public static void close(InputStream is,OutputStream os){
close(is);
close(os);
}
}
|
SocketUtil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| public class SocketUtil {
//关闭Socket
public static void close(Socket socket){
if(socket != null && !socket.isClosed()){
try{
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
//关闭ServerSocket
public static void close(ServerSocket ss){
if(ss != null && !ss.isClosed()){
try{
ss.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
|