团队名称、团队成员介绍、任务分配,团队成员课程设计博客链接(以表格形式呈现),标明组长。
团队名称:三点起床敲代码
团队成员介绍:
姓名 | gitee账号 | 任务分配 |
---|---|---|
王奥运(组长) | AAAAysy | 客户端多线程,css修饰客户端,客户端方法 |
陈伟盛 | 晚点睡觉 | GUI界面,点击事件,页面逻辑,客户端方法 |
张小杰 | 代码小白12 | 客户端与服务端网络通信,处理数据,服务端多线程 |
胡华 | 早点睡觉 | 数据库应用,数据处理,服务端多线程 |
本项目的git地址。
客户端:https://gitee.com/aaaaysy/achat-client.git
服务端:https://gitee.com/code-white-12/achat-server.git
项目git提交记录截图(要体现出每个人的提交记录及提交说明)
前期调查(主要包含别人做得怎么样、怎么做的内容)
我们将制作一个通信软件,我们参考当下最流行的社交软件微信和QQ,并从中吸取了一些精华。聊天工具最基本功能就是即时通讯,包括对好友私聊和群聊,将通过服务器转发,并存储聊天记录在服务器端的数据库。区分不同的用户,用不同的账号密码进行登录请求,在此之外还要有增删好友和进退群聊的功能,用户信息和好友信息也存放在数据库。
项目功能架构图、主要功能流程图
客户端框架:
服务端框架
发送消息
添加好友:
删除好友:
加入群聊:
面向对象设计包图、类图。如果类太多,可仅列出主要类。
客户端:
View包:图形界面包
main包:启动程序
utiles包:工具包
messagetype包:消息包
image包:界面资源文件
服务端:
lib包:资源包
messagetype包:消息包
pojo包:用户
photo包:图片资源
resources包:资源包
项目运行截图或屏幕录制
登录界面
密码错误反馈
注册界面
头像可以拖拉,也可以填写地址或网络地址
生日选择框
注册成功,返回账号
聊天界面
群聊界面
群聊主页
群成员列表
邀请好友进群
好友界面
资料详情
个人资料
好友资料
添加新好友
几种反馈
新的好友请求(别人发来的好友请求)
建立新的群聊
创建成功的反馈
项目关键代码分模块描述:模块名称-文字说明-关键代码(注意:不得直接复制所有源代码,仅粘贴最关键代码)。
客户端主界面
package view.controller;
/**
* @author
*/
public class MainWindowController implements Initializable {
@FXML
public CheckBox checkBox1;
@FXML
public CheckBox checkBox2;
@FXML
private ListView<Object> groupChatListView;
@FXML
private ListView<Object> groupMessageListView;
@FXML
private TextArea groupTestArea;
@FXML
private Button groupSendButton;
@FXML
public Label groupName;
@FXML
private ListView<Object> friendListView;
@FXML
private ListView<Object> friendChatListView;
@FXML
private ListView<Object> friendMessageListView;
@FXML
private TextField url;
@FXML
private ImageView imageView;
@FXML
private Button loadImage;
@FXML
private Button setHead;
@FXML
private Tab headTab;
@FXML
private Button sendButton;
@FXML
private Label gender;
@FXML
private Label diqu;
@FXML
private Label gender1;
@FXML
private Label beizhu1;
@FXML
private ImageView headphoto;
@FXML
private Label account1;
@FXML
private Label beizhu;
@FXML
private Label diqu1;
@FXML
private Label account;
@FXML
private Label userName1;
@FXML
private Label friendName;
@FXML
private TabPane tabPane;
@FXML
private Tab tab3;
@FXML
private Tab tab1;
@FXML
private Tab tab2;
@FXML
private TextArea testArea;
public static ObservableList<Object> friendList1;
public static ObservableList<Object> chatList1;
public static ObservableList<Object> groupList1;
public static List<User> friends;
public static User chooseUser;
public static ObservableList<Object> chooseFriendMessagePage;
public static ArrayList<ObservableList<Object>> friendMessagePageList = new ArrayList<>();
public static List<Group> groups;
public static Group chooseGroup;
public static ObservableList<Object> chooseGroupMessagePage;
public static ArrayList<ObservableList<Object>> groupMessagePageList = new ArrayList<>();
@SneakyThrows
@Override
public void initialize(URL location, ResourceBundle resources) {
MainWindow.getMainWindow().setFriendListView(friendListView);
MainWindow.getMainWindow().setGroupName(groupName);
MainWindow.getMainWindow().setFriendName(friendName);
MainWindow.getMainWindow().setHeadTab(this.headTab);
tabPane.setTabMaxHeight(200);
tabPane.setTabMaxWidth(200);
tabPane.getSelectionModel().select(tab1);
ImageView iv = new ImageView("file:photo/" + MainWindow.getMainWindow().getUser().getAccount() + ".png");
iv.setFitHeight(50);
iv.setFitWidth(50);
headTab.setGraphic(iv);
headTab.setStyle("-fx-background-color: rgb(51,51,51)");
headTab.setText(" ");
setTabImage(tab1,"image/mainWindow/vx.png");
setTabImage(tab2,"image/mainWindow/friend.png");
setTabImage(tab3,"image/mainWindow/group.png");
int y=265,x=55;
tabPane.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getSceneY() <= y && event.getSceneX() <= x) {
tabPane.setCursor(Cursor.HAND);
} else {
tabPane.setCursor(Cursor.DEFAULT);
}
}
});
friendListView.setItems(friendList1);
friendChatListView.setItems(chatList1);
groupChatListView.setItems(groupList1);
friendListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
int num = friendListView.getSelectionModel().getSelectedIndex();//获取点击的序号
User user;
int bound=4;
int addNewFriend=1;
int newFriend=2;
int createGroup=3;
if (num < bound) {
user = MainWindow.getMainWindow().getUser();
} else {
user = friends.get(num - 4);
}
if (num == addNewFriend) {
new AddNewFriend();
}else if (num == newFriend) {
new NewFriend();
try {
NewFriend.getNewFriend().fun();
} catch (IOException e) {
e.printStackTrace();
}
}else if(num==createGroup){
new CreateGroup();
}
userName1.setText(user.getName());//用户名
account1.setText(user.getAccount());//账号
diqu1.setText(user.getAddress());//地址
gender1.setText(user.getSex());//性别
beizhu1.setText("无");//备注
Image p = new Image(String.format("file:photo/%s.png", user.getAccount()));
headphoto.setImage(p);//头像
});
//点击加载消息
friendChatListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
int num = friendChatListView.getSelectionModel().getSelectedIndex();//获取点击的序号
chooseUser = friends.get(num);//获取该消息信息
chooseFriendMessagePage = friendMessagePageList.get(num);
friendMessageListView.setItems(chooseFriendMessagePage);
friendMessageListView.scrollTo(chooseFriendMessagePage.size() - 1);
friendName.setText(chooseUser.getName());
});
//点击加载群
groupChatListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
int num = groupChatListView.getSelectionModel().getSelectedIndex();
try {
chooseGroup = groups.get(num);
chooseGroupMessagePage = groupMessagePageList.get(num);
}catch (Exception e){
}
groupMessageListView.setItems(chooseGroupMessagePage);
groupMessageListView.scrollTo(chooseGroupMessagePage.size() - 1);
groupName.setText(chooseGroup.getGroupName());
});
}
public void setTabImage(Tab tab,String url){
ImageView iv=new ImageView(url);
iv.setFitHeight(35);
iv.setFitWidth(35);
tab.setGraphic(iv);
tab.setText(" ");
}
public void sendMessageToFriend(MouseEvent event) {
int num = friends.indexOf(chooseUser);
friendMessageListView.setItems(chooseFriendMessagePage);
String message = testArea.getText();
testArea.clear();
if(checkBox1.isSelected()) {
message=message.substring(0,message.length()-1);
}
chooseFriendMessagePage.add(new MessageBubble().setTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
chooseFriendMessagePage.add(new MessageBubble().setOnRight(message, MainWindow.getMainWindow().getUser().getAccount()));
if (num > 0) {
friendList1.add(4,MainWindowController.friendList1.remove(num+4));
chatList1.add(0, chatList1.remove(num));
friends.add(0, friends.remove(num));
friendMessagePageList.add(0, friendMessagePageList.remove(num));
}
friendChatListView.getSelectionModel().selectFirst();
chooseFriendMessagePage = friendMessagePageList.get(0);
friendMessageListView.scrollTo(chooseFriendMessagePage.size() - 1);
Connect connect = Connect.getConnect();
try {
connect.sendMessageToFriend(MainWindow.getMainWindow().getUser(), chooseUser, message);
System.out.println("发送消息完成");
} catch (IOException e) {
System.out.println("发送消息失败");
}
}
public void sendMessageToGroup(MouseEvent event) {
int num = groups.indexOf(chooseGroup);
groupMessageListView.setItems(chooseGroupMessagePage);
String message = groupTestArea.getText();
groupTestArea.clear();
if(checkBox2.isSelected()) {
message=message.substring(0,message.length()-1);
}
chooseGroupMessagePage.add(new MessageBubble().setTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
chooseGroupMessagePage.add(new MessageBubble().setOnRight(message, MainWindow.getMainWindow().getUser().getAccount()));
if (num > 0) {
groupList1.add(0, groupList1.remove(num));
groups.add(0, groups.remove(num));
groupMessagePageList.add(0, groupMessagePageList.remove(num));
}
groupChatListView.getSelectionModel().selectFirst();
chooseGroupMessagePage = groupMessagePageList.get(0);
groupMessageListView.scrollTo(chooseGroupMessagePage.size() - 1);
Connect connect = Connect.getConnect();
try {
connect.sendMessageToGroup(MainWindow.getMainWindow().getUser(), chooseGroup, message);
System.out.println("发送消息完成");
} catch (IOException e) {
System.out.println("发送消息失败");
}
}
@FXML
private void getImage(MouseEvent event) {
if (url.getText() != null) {
Image image = new Image(url.getText());
imageView.setImage(image);
}
}
public void loadImage(DragEvent dragEvent) {
Dragboard dragboard = dragEvent.getDragboard();
if (dragboard.hasFiles()) {
String path = dragboard.getFiles().get(0).getAbsolutePath();
path = "file:" + path;
url.setText(path);
this.getImage(null);
}
}
public void setMouse(DragEvent dragEvent) {
dragEvent.acceptTransferModes(TransferMode.ANY);
}
@FXML
public void testAreaKeyPressed(KeyEvent keyEvent) {
if (checkBox1.isSelected() && keyEvent.getCode() == KeyCode.ENTER) {
this.sendMessageToFriend(null);
}
}
@FXML
public void groupTestAreaKeyPressed(KeyEvent keyEvent) {
if (checkBox2.isSelected() && keyEvent.getCode() == KeyCode.ENTER) {
this.sendMessageToGroup(null);
}
}
}
服务端主方法
import MessageType.*;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.*;
import java.net.Socket;
import java.util.*;
import org.junit.Test;
import pojo.User;
public class MainFunction {
private static ArrayList<ServerThread> stList = new ArrayList<ServerThread>();//在线用户集合
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
Socket socket = null;
try {
socket = new Socket("192.168.123.169", 9989);
} catch (IOException e) {
e.printStackTrace();
}
InputStream is = socket.getInputStream();
OutputStream bs = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(bs);
DataInputStream dis = new DataInputStream(is);
byte type2 = 0x01;
User user=new User("赵洋5d","123456",10,"男","福建","123465",new ArrayList<>());
String str = JSON.toJSONString(user);
int len1 = 5 + str.getBytes().length;
RegMsg rm = new RegMsg(len1, type2, str);
byte[] data = MsgTool.packMsg(rm);
dos.write(data);
dos.flush();
MSHead msg = MsgTool.readMsgHead(dis);
byte type1 = msg.getType();
User user1 = null;
if (type1 == 0x05) {
RegResMsg lrm = (RegResMsg) msg;
user1 = JSON.parseObject(lrm.getJsonStr(), User.class);
System.out.println("账号:"+user1.getAccount());
System.out.println("密码:"+user1.getPassword());
Byte state = lrm.getState();
if (state == 0) {
System.out.println("注册成功");
}
}
byte type4 = 0x04;
String jsonStr = JSON.toJSONString(user1);
FileMsg fM = new FileMsg(5+jsonStr.getBytes().length,type4,jsonStr);
byte[] bytes = MsgTool.packMsg(fM);
dos.write(bytes);
dos.flush();
System.out.println("已连接到服务器9999端口,准备传送图片...");
//获取图片字节流
FileInputStream fis = new FileInputStream("src\\photo\\t0.jpg");
//获取输出流
//OutputStream out = s.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(socket.getOutputStream());
byte[] buf = new byte[1024];
int len = 0;
//2.往输出流里面投放数据
while ((len = fis.read(buf)) != -1)
{
bos.write(buf,0,len);
}
bos.flush();
socket.shutdownOutput();
// byte type1 = 0x02;
// User user = new User();
// user.setAccount("123456");
// user.setPassword("123456");
// String jsonStr = JSON.toJSONString(user);
// LoginMsg lm = new LoginMsg(5+jsonStr.getBytes().length,type1,jsonStr);
// byte[] bytes = MsgTool.packMsg(lm);
// dos.write(bytes);
// dos.flush();
// MSHead msg = MsgTool.readMsgHead(dis);
// byte type1 = msg.getType();
// if(type1==0x06){
// LoginResMsg lrm =(LoginResMsg) msg;
// byte state = lrm.getState();
// if(state == 0) {
// System.out.println("登录成功");
// }
// }
// byte type2 = 0x03;
// String message = "1as65dasd4";
// ChatMsg cm = new ChatMsg(5+message.getBytes().length,type2,message);
// byte[] bytes1 = MsgTool.packMsg(cm);
// dos.write(bytes1);
// dos.flush();
// int len;
// byte[] bytes2 = new byte[1024];
// while(true){
// while((len=dis.read(bytes2))!=-1){
// String message2 = new String(bytes2,0,len);
// System.out.println(message2);
// }
// }
}//测试方法
public static boolean checkReg(String name, String account, String password) {
return true;
}//注册验证
public static boolean checkLogin(User user) {
UserMapper userMapper = loadUserMapperXML();
List<User> users = userMapper.checkLogin(user.getAccount(), user.getPassword());
if (!users.isEmpty()) {
return true;
} else {
return false;
}
}//登录验证
public static UserMapper loadUserMapperXML() {
String resource = "resources/mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);//设置事务自动提交
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
return usermapper;
}
public static CompanionMapper loadCompanionMapperXML() {
String resource = "resources/mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);//设置事务自动提交
CompanionMapper companionMapper = sqlSession.getMapper(CompanionMapper.class);
return companionMapper;
}
//注册后保存用户信息
public static User saveInformation(User user) {
UserMapper userMapper = loadUserMapperXML();
// User user = new User(account,name,password,age,sex,head,address,label,phone,background);
// int i = userMapper.saveUser(user);
userMapper.saveUser(user);
return user;
}//保存用户信息
public static void updateUser() {
UserMapper userMapper = loadUserMapperXML();
}//更新用户信息
public static void deleteUser() {
}//踢出(删除)用户
public static void parseImage(Socket s,User user){
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\photo\\"+user.getAccount()+".jpg"));
BufferedInputStream bis =new BufferedInputStream(s.getInputStream());
byte[] b =new byte[2048];
int len =-1;
int count=0;
while((len = bis.read(b))!=-1){
count+=len;
bos.write(b,0,len);
}
bis.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void returnImage(Socket s,String account) {
try {
System.out.println(account);
String s1=String.format("src/photo/ %s.jpg",account);
String s2 ="src/photo/"+account+".jpg";
System.out.println(s1);
FileInputStream fis = new FileInputStream(new File("src/photo/123458.jpg" ));
BufferedOutputStream bos=new BufferedOutputStream(s.getOutputStream());
byte[] buf = new byte[1024*6];
int len = -1;
//2.往输出流里面投放数据
while ((len = fis.read(buf)) != -1)
{
bos.write(buf,0,len);
}
bos.flush();
s.shutdownOutput();
fis.close();
//bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test() {
// UserMapper userMapper = loadUserMapperXML();
// User user=new User("徐家棚","123456",10,"男","福建","123465",new ArrayList<>());
String s = userMapper.selectAccount(user.getName());
// System.out.println(s);
}
}
客户端和服务端的通信
package utils;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import messagetype.*;
import java.io.*;
import java.net.Socket;
/**
* @author
*/
@Data
public class Connect {
private static Connect connect;
private static final String IP = "106.55.180.124";
private static final int PORT = 9999;
private Socket socket = null;
DataInputStream dis = null;
DataOutputStream dos = null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
private Connect() throws IOException {
}
static {
try {
connect = new Connect();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connect getConnect() {
return connect;
}
public String register(User user) throws IOException {
byte type2 = 0x01;
String str = JSON.toJSONString(user);
int len1 = 5 + str.getBytes().length;
RegMsg rm = new RegMsg(len1, type2, str);
byte[] data = MsgTool.packMsg(rm);
dos.write(data);
dos.flush();
MsHead msg = MsgTool.readMsgHead(dis);
byte type1 = msg.getType();
User user1 = null;
if (type1 == ByteType.REGISTER_RES_MESSAGE) {
RegResMsg lrm = (RegResMsg) msg;
user1 = JSON.parseObject(lrm.getJsonStr(), User.class);
Byte state = lrm.getState();
if (state == 0) {
System.out.println("注册成功");
}
}
byte type4 = ByteType.SAVE_IMAGE_MESSAGE;
String jsonStr = JSON.toJSONString(user1);
messagetype.FileMsg fM = new messagetype.FileMsg(5+jsonStr.getBytes().length,type4,jsonStr);
byte[] bytes = MsgTool.packMsg(fM);
dos.write(bytes);
dos.flush();
File file=new File("photo/temp.png");
FileInputStream fis = new FileInputStream(file);
//获取输出流
bos=new BufferedOutputStream(socket.getOutputStream());
byte[] buf = new byte[1024];
int len = 0;
int count=0;
//2.往输出流里面投放数据
while ((len = fis.read(buf)) != -1)
{
count+=len;
bos.write(buf,0,len);
}
bos.flush();
socket.shutdownOutput();
fis.close();
bos.close();
dos.close();
file.delete();
return user1.getAccount();
}
public boolean startConnect() {
try {
socket = new Socket(IP, PORT);
this.dos = new DataOutputStream(socket.getOutputStream());
this.dis = new DataInputStream(socket.getInputStream());
System.out.println("连接成功");
return true;
} catch (IOException e) {
System.out.println("连接失败");
return false;
}
}
public void sendMessageToFriend(User srcUser, User desUser, String message) throws IOException {
UserMsgToUser um = new UserMsgToUser(srcUser,desUser,message);
String privateMsg = JSON.toJSONString(um);
byte type = 0x03;
ChatMsg cm = new ChatMsg(privateMsg.getBytes().length+5,type,privateMsg);
byte[] bytes = MsgTool.packMsg(cm);
dos.write(bytes);
dos.flush();
}
public void sendMessageToGroup (User srcUser,Group desGroup,String message) throws IOException{
byte type = 0x17;
String groupAccount = desGroup.getGroupAccount();
UserMsgToGroup um = new UserMsgToGroup(message,groupAccount,srcUser);
String information = JSON.toJSONString(um);
TogetherChatMsg tt = new TogetherChatMsg(5+information.getBytes().length,type,information);
byte[] bytes = MsgTool.packMsg(tt);
dos.write(bytes);
dos.flush();
}
public User login(String account, String password) throws IOException {
byte type = 0x02;
User user = new User();
user.setAccount(account);
user.setPassword(password);
String string = JSON.toJSONString(user);
int len = 5+string.getBytes().length;
LoginMsg rm = new LoginMsg(len, type, string);
byte[] data = MsgTool.packMsg(rm);
dos.write(data);
dos.flush();
MsHead msg = MsgTool.readMsgHead(dis);
byte type1 = msg.getType();
if (type1 == ByteType.LOGIN_RES_MESSAGE) {
LoginResMsg rrm = (LoginResMsg) msg;
Byte state = rrm.getState();
String userStr = rrm.getJsonStr();
System.out.println(userStr);
if (state == 0) {
return JSON.parseObject(userStr, User.class);
}
}
return null;
}
public void getUserByAccount(String account,int flag) throws IOException {
//flag=0 or 1;
byte type = 0x07;
FindMsg fm = new FindMsg(flag,9+account.getBytes().length, type,account);
byte[] bytes = MsgTool.packMsg(fm);
dos.write(bytes);
dos.flush();
}
public void getNewFriends(String account) throws IOException {
byte type = 0x10;
FindResList frl = new FindResList(account.getBytes().length+5,type,account);
byte[] bytes = MsgTool.packMsg(frl);
dos.write(bytes);
dos.flush();
}
public boolean getHeadByAccount(String account) throws IOException {
File file=new File(String.format("photo/%s.png",account));
if(file.exists()){
return false ;
}
byte type = ByteType.FIND_IMAGE_MESSAGE;
AccountAndImage aai = new AccountAndImage(account,null);
String accountAndImage = JSON.toJSONString(aai);
FindImageMsg fim = new FindImageMsg(5+accountAndImage.getBytes().length,type,accountAndImage);
byte[] bytes = MsgTool.packMsg(fim);
dos.write(bytes);
dos.flush();
return true;
}
public void addFriend(User me,User friend) throws IOException {
UserToUser userToUser=new UserToUser(me,friend);
byte type = 0x09;
String json = JSON.toJSONString(userToUser);
AddFriendMsg afm = new AddFriendMsg(5+json.getBytes().length,type,json);
byte[] bytes = MsgTool.packMsg(afm);
dos.write(bytes);
dos.flush();
}
public void handFriendRequests(User me, User friend, byte state) throws IOException {
UserToUser userToUser = new UserToUser(me,friend);
String json = JSON.toJSONString(userToUser);
byte type = 0x11;
AddFriendResMsg afrm = new AddFriendResMsg(6+json.getBytes().length,type,state,json);
byte[] bytes = MsgTool.packMsg(afrm);
dos.write(bytes);
dos.flush();
System.out.println("已同意");
}
public void getFriendMessageList(User user) throws IOException {
byte type = 0x12;
String jsonUser = JSON.toJSONString(user);
FriendMsgList fml = new FriendMsgList(5+jsonUser.getBytes().length,type,jsonUser);
byte[] bytes = MsgTool.packMsg(fml);
dos.write(bytes);
dos.flush();
}
public void getGroupMessageList(User user) throws IOException {
byte type = ByteType.GROUP_MESSAGE_LIST;
String jsonGroupMsg = JSON.toJSONString(user);
GroupMsgList gm = new GroupMsgList(5+jsonGroupMsg.getBytes().length,type,jsonGroupMsg);
byte[] bytes = MsgTool.packMsg(gm);
dos.write(bytes);
dos.flush();
}
public void delete(User srcUser,User desUser) throws IOException {
byte type = ByteType.DELETE_FRIEND_MESSAGE;
UserToUser utu=new UserToUser(srcUser,desUser);
String jsonUser = JSON.toJSONString(utu);
DeleteFriendMsg dfm = new DeleteFriendMsg(5+jsonUser.getBytes().length,type,jsonUser);
byte[] bytes = MsgTool.packMsg(dfm);
dos.write(bytes);
dos.flush();
}
public void exitGroup(User user, Group group) throws IOException {
System.out.println(group);
byte type = ByteType.EXIT_GROUP_MESSAGE;
ExitGroup eg = new ExitGroup(group,user);
String json = JSON.toJSONString(eg);
ExitGroupMsg egm = new ExitGroupMsg(5+json.getBytes().length,type,json);
byte[] bytes = MsgTool.packMsg(egm);
dos.write(bytes);
dos.flush();
}
public void createGroup(User user, Group group) throws IOException {
byte type = ByteType.BUILD_GROUP_MESSAGE;
String str = null;
try {
str = ImgUtil.encodeImage("photo/temp/temp.png");
} catch (IOException e) {
e.printStackTrace();
}
group.setImage(str);
String jsonGroup = JSON.toJSONString(group);
CreateGroupMsg cg = new CreateGroupMsg(5+jsonGroup.getBytes().length,type,jsonGroup);
byte[] bytes = MsgTool.packMsg(cg);
dos.write(bytes);
dos.flush();
}
public void disconnect() throws IOException {
if(bos!=null){
bos.close();
}
if(bis!=null){
bis.close();
}
if(dis!=null){
dis.close();
}
if(dos!=null){
dos.close();
}
socket.close();
}
public void userAccessToGroup(User desUser, Group group) throws IOException {
byte type = ByteType.ADD_USER_TO_GROUP_MESSAGE;
UserGroupToUser ug = new UserGroupToUser(desUser,group);
String json = JSON.toJSONString(ug);
AddUserToGroupMsg aut = new AddUserToGroupMsg(5+json.getBytes().length,type,json);
byte[] bytes = MsgTool.packMsg(aut);
dos.write(bytes);
dos.flush();
}
}
项目代码扫描结果及改正。
考核前请务必在IDE中使用阿里巴巴代码规范扫描插件对项目代码进行扫描,并解决项目中的所有警告。
负责编写博客的同学请在博客中列出改正前2-3个不同类型的扫描警告结果,并描述改正过程,请放入团队博客3.9。
1、在if/else/for/while/do语句中必须使用大括号,即使只有一行代码,避免使用下面的形式:if (condition) statements。
给if加上{}
2、不允许任何魔法值(即未经定义的常量)直接出现在代码中。
用枚举将魔法值定义为常量。
3、Object的equals方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals。
翻转equals的调用。
4、方法名、参数名、成员变量、局部变量都统一使用lowerCamelCase,必须遵从驼峰形式。
修改命名规则。
5、单个方法的总行数不超过80行。 说明:除注释之外的方法签名、结束右大括号、方法内代码、空行、回车及任何不可见字符的总行数不超过80行。
将一些功能封装成方法,尽量减少一个方法中的代码行数
项目总结感想
第一次多人合作一个大的项目,前期因为没有正确梳理需求,没有划分好每个人的分工,缺乏沟通交流,在代码实现的过程中遇到了很多问题,甚至不乏争吵客户端和服务端之间传输数据包遇到的问题,文件的存储路径问题,多线程相争问题,jar包封装问题,服务器最终部署到云端问题,不过最后项目也成功运行了。项目完成时间较为仓促,项目中有许多不足,有许多未能完成的功能,期待日后的完善。最后感谢整个小组成员的共同努力,完成了此次课程设计。