1.项目简介,涉及结束
用户打开应用,登录成功后进入主界面,有聊天和联系人分页,可以群聊和私聊。
涉及技术:
netty
javafx
mybatis
spring boot
protobuf
mysql
2.项目git地址
服务端:https://gitee.com/ltm77/chatserver
客户端:https://gitee.com/ltm77/chatclient
3.项目git提交记录截图
4.项目功能架构图、主要包关系图
5.项目运行截图
6.项目关键代码
服务端消息派发
public void userOrderDispatch(ChannelHandlerContext ctx, UserOrder.OrderMessage msg){
UserOrder.OrderMessage.OrderType orderType=msg.getOrderType();
switch (orderType){
case UserLoginType:
userLoginTypeHandler(ctx,msg);
break;
case AddConversationType:
addConversationHandler(ctx,msg);
break;
case SendPersonalChatMessageType:
sendPersonalChatMessageHandler(ctx,msg);
break;
case SendGroupChatMessageType:
sendSendGroupChatMessageHandler(ctx,msg);
break;
case RemoveConversationType:
removeConversationHandler(ctx,msg);
break;
default:
}
}
服务端转发私聊消息
public void sendPersonalChatMessage(UserOrder.SendPersonalChatMessage sendPersonalChatMessage){
String account = sendPersonalChatMessage.getAccount();
String linkmanAccount = sendPersonalChatMessage.getLinkmanAccount();
UserEntiny.PersonalChatMessage personalChatMessage = sendPersonalChatMessage.getPersonalChatMessage();
String messageContent = personalChatMessage.getMessageContent();
long timeStamp = personalChatMessage.getTimeStamp();
//需要插入两个聊天记录,一个是发送者的记录,一个是接收者的记录
PersonalChatMessage personalChatMessageTemp = new PersonalChatMessage();
personalChatMessageTemp.setMessageContent(messageContent);
personalChatMessageTemp.setSign(1);
personalChatMessageTemp.setTimeStamp(timeStamp);
userServer.insertPersonalChatMessage(personalChatMessageTemp,account,linkmanAccount);
personalChatMessageTemp.setSign(2);
userServer.insertPersonalChatMessage(personalChatMessageTemp,linkmanAccount,account);
Channel linkmanAccountChannel = nettyOnlineServer.findUser(linkmanAccount);
if(linkmanAccountChannel!=null){
UserEntiny.PersonalChatMessage.Builder p=UserEntiny.PersonalChatMessage.newBuilder()
.setTimeStamp(timeStamp)
.setMessageContent(messageContent)
.setMessageFlag(1)
.setSign(2)
.setId(personalChatMessageTemp.getId());
UserOrder.SendPersonalChatMessage.Builder s = UserOrder.SendPersonalChatMessage.newBuilder()
.setAccount(account)
.setPersonalChatMessage(p)
.setLinkmanAccount(linkmanAccount);
UserOrder.OrderMessage.Builder orderMessage = UserOrder.OrderMessage.newBuilder();
orderMessage.setOrderType(UserOrder.OrderMessage.OrderType.SendPersonalChatMessageType)
.setSendPersonalChatMessage(s);
linkmanAccountChannel.writeAndFlush(orderMessage.build());
System.out.println("转发消息成功");
}
else {
userServer.insertConversation(linkmanAccount,2,"0",account);
userServer.insertPersonalConversation(linkmanAccount,account);
userServer.insertPersonalUnreadMessage(1,account,linkmanAccount,personalChatMessageTemp.getId());
}
}
客户端删除会话事件
void removeConversation(ActionEvent event) {
UserOrderServer userOrderServer = (UserOrderServer) SpringContextUtil.getBean("userOrderServer");
User currentUser = (User) SpringContextUtil.getBean("currentUser");
Conversation conversation = conversationJFXListView.getSelectionModel().getSelectedItem();
if (conversation == null) return;
int type = conversation.getType();
String groupAccount = "0";
String linkmanAccount = "0";
if (type == 1) {
groupAccount = conversation.getGroupConversation().getCurrentChatGroup().getGroupAccount();
} else {
linkmanAccount = conversation.getPersonalConversation().getCurrentLinkman().getAccount();
}
ObservableList conversationObservableList = (ObservableList) SpringContextUtil.getBean("conversationObservableList");
conversationObservableList.remove(conversation);
conversationJFXListView.getSelectionModel().clearSelection();
setCurrentConversation(null);
userOrderServer.removeConversation(currentUser.getAccount(), type, groupAccount, linkmanAccount);
}
7.项目代码扫描结果及改正
大部分警告是命名不规范和if没有大括号,注解没有使用正确
8.尚待改进
由于时间紧迫,很多功能没有实现,后续功能有时间会完成,这是一个长期支持的项目