生活随笔
收集整理的這篇文章主要介紹了
基于Java的实时聊天系统设计与实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第四章 系統設計
系統設計是把用戶需求轉化為系統的最重要開發環節,解決了“應該怎么做系統”的問題。在本章節中,主要是在系統需求分析的基礎上,對系統架構、系統功能模塊設計、系統工作流程設計和系統數據庫設計進行闡述。
4.1 系統架構
實時聊天系統采用SpringBoot+Free Marker+Jpa框架開發,是標準的MVC模式,將整個系統劃分為View層,Controller層,Service層,DAO層四層。其中,Free Marker拿取數據進行展示數據,SpringBoot實現業務對象管理,Jpa作為數據對象的持久化引擎。整個系統架構運行流程如圖4-1所示:
圖4-1 系統架構運行圖
View層:與Controller層結合比較緊密,需要二者結合起來協同工發,主要負責前臺ftl頁面的表示。
Controller層:控制器,導入service層,因為service中的方法是我們使用到的,controller通過接收前端傳過來的參數進行業務操作,在返回一個指定的路徑或者數據表。
Service層:存放業務邏輯處理,也是一些關于數據庫處理的操作,但不是直接和數據庫打交道,它有接口還有接口的實現方法,在接口的實現方法中需要導入Dao層,Dao層是直接跟數據庫打交道的,它也是個接口,只有方法名字,具體實現在mapper.xml文件里,service是供我們使用的方法。
Dao層:負責對數據向數據庫增刪改查的操作。在該注冊的框架中,如果不使用SpringBoot的話,每個層之間的數據傳遞都需要new一個調用該層數據的類的實例。而使用了SpringBoot的話,需要做的就是把DAO層和BIZ層的每個類都寫一個接口類,接口類里寫實現類的方法,在調用的時候不new對象,直接用對象點(.)方法就可以,還需要對每個對象加上set/get方法。
持久層:使用了Jpa來將實體對象持久化到數據庫中。不用再進行繁雜的Jdbc和sql語句。在Dao層使用Jpa語法可以直接使用想要進行的sql,或者可以直接加上@Query注解后面寫要進行的sql語句
4.2 系統功能模塊設計
實質上,實時聊天系統的綜合性相對較強,復雜程度相對較高,可對現有軟件進行充分利用,進行系統設計與規劃。構建完善成熟的實時聊天系統,其中涉及到以下內容,即前臺網頁界面、處理程序、MySQL 后臺數據庫系統等,在網站頁面中顯示出以下內容,例如用戶頭像、群組名稱、群組成員、群組信息等。處理程序其實也就是對用戶提交表單與相關操作進行處理,存儲在后臺數據庫的信息有賬戶數據、聊天記錄數據、群組數據和新聞數據等。
因此,在線系統需要具備前臺功能和后臺功能,其中,前臺功能實現以下功能,用戶注冊、用戶登錄、添加好友、加入群聊、創建群聊、查看聊天記錄、發送消息、刪除好友、退出群聊、解散群聊和個人設置。系統前臺功能如圖4-2所示:
圖4-2 系統前臺功能模塊結構圖
系統后臺功能實現以下功能,聊天管理和系統設置。系統后臺功能如圖4-3所示:
圖4-3 系統后臺功能模塊結構圖
目錄
摘 要 I
ABSTRACT II
第一章 緒論 1
1.1 課題背景、目的及意義 1
1.1.1 課題背景 1
1.1.2 課題目的和意義 2
第二章 相關技術介紹 3
2.1 Javascript 3
2.2 Ajax 3
2.3 MySQL 3
2.4 SpringBoot框架 3
2.5 Free Marker模板引擎 4
2.6 B/S模式 4
2.8 系統開發平臺及運行環境 5
2.7.1 系統開發平臺 5
2.7.2 運行環境 6
第三章 系統需求分析 8
3.1 功能需求分析 8
3.2 非功能需求分析 11
3.3 可行性分析 12
3.3.1 經濟可行性 12
3.3.2 技術可行性 12
3.3.3 操作可行性 13
第四章 系統設計 14
4.1 系統架構 14
4.2 系統功能模塊設計 15
4.3 系統工作流程設計 16
4.4 數據庫設計 16
4.4.1 數據庫概念設計 17
4.4.2 數據庫邏輯設計 24
第五章 系統實現 30
5.1 關鍵代碼 30
5.2 用戶模塊 49
5.2.1 好友申請 49
5.2.2 好友設置 52
5.2.3 注冊登錄 54
5.2.4 發送消息 55
5.2.5 個人中心 57
5.2.6 設置 59
5.2.7 群組管理 59
5.3 管理員模塊 62
5.3.1 管理員登錄 62
5.3.2 聊天管理 62
5.3.3 系統設置 64
5.3.4 個人信息 66
第六章 系統測試 67
6.1 測試的目的與目標 67
6.2 測試方法 67
6.3 測試用例 68
6.4 測試結論 68
結論與展望 70
致 謝 71
參考文獻 72
部分代碼參考:
package com
.yuanlrc
.base.server
.home
;import java
.io
.IOException
;
import java
.util
.List
;
import java
.util
.Map
;
import java
.util
.concurrent
.ConcurrentHashMap
;import javax
.websocket
.OnClose
;
import javax
.websocket
.OnError
;
import javax
.websocket
.OnMessage
;
import javax
.websocket
.OnOpen
;
import javax
.websocket
.Session
;
import javax
.websocket
.server
.PathParam
;
import javax
.websocket
.server
.ServerEndpoint
;import org
.apache
.commons
.lang3
.StringUtils
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;import com
.alibaba
.fastjson
.JSONObject
;
import com
.yuanlrc
.base.bean
.WebSocketMsg
;
import com
.yuanlrc
.base.entity
.common
.Account
;
import com
.yuanlrc
.base.entity
.common
.AccountGroupMember
;
import com
.yuanlrc
.base.entity
.common
.Friend
;
import com
.yuanlrc
.base.entity
.common
.MsgContent
;
import com
.yuanlrc
.base.entity
.common
.MsgLog
;
import com
.yuanlrc
.base.service
.common
.AccountGroupMemberService
;
import com
.yuanlrc
.base.service
.common
.AccountService
;
import com
.yuanlrc
.base.service
.common
.FriendService
;
import com
.yuanlrc
.base.service
.common
.MsgContentService
;
import com
.yuanlrc
.base.service
.common
.MsgLogService
;@Service
@
ServerEndpoint("/webSocket/{userid}")
public class WebSocket {public static Map<Long, WebSocket> clients
= new ConcurrentHashMap<Long, WebSocket>(); public static int onlineCount
= 0;private Long userid
;private Session session
;private List<Friend> friendList
;private static AccountService accountService
;private static FriendService friendService
;private static MsgContentService msgContentService
;private static MsgLogService msgLogService
;private static AccountGroupMemberService accountGroupMemberService
;@OnOpen
public void onOpen(@
PathParam("userid") Long userid
, Session session
) throws IOException
{ this.userid
= userid
;this.session
= session
;this.friendList
= friendService
.findMyFriendList(userid
);addOnlineCount(); clients
.put(userid
, this);System
.out.println("成功建立連接,用戶ID = 【" + userid
+ "】,當前在線用戶數:" + onlineCount
);loadUnReadMsg(userid
);onlineNotice(userid
);} @OnClose
public void onClose() throws IOException
{ clients
.remove(userid
); subOnlineCount();offlineNotice(userid
);System
.out.println("已斷開連接,用戶ID = 【" + userid
+ "】,當前在線用戶數:" + onlineCount
);} @OnMessage
public void onMessage(String message
) throws IOException
{ WebSocketMsg webSocketMsg
= JSONObject
.parseObject(message
, WebSocketMsg
.class);System
.out.println(webSocketMsg
);sendMsg(webSocketMsg
);} @OnError
public void onError(Session session
, Throwable error
) { error
.printStackTrace(); } public void sendMessageAll(String message
) throws IOException
{ for (WebSocket item
: clients
.values()) { item
.session
.getAsyncRemote().sendText(message
); } } public static synchronized
int getOnlineCount() { return onlineCount
; } public static synchronized
void addOnlineCount() { WebSocket
.onlineCount
++; } public static synchronized
void subOnlineCount() { WebSocket
.onlineCount
--; } public static synchronized
Map<Long, WebSocket> getClients() { return clients
;}public void sendMsg(WebSocketMsg webSocketMsg
){if(WebSocketMsg
.CHAT_TYPE_SINGLE
.equals(webSocketMsg
.getChatType())){sendSingleMsg(webSocketMsg
);return;}if(WebSocketMsg
.CHAT_TYPE_GROUP
.equals(webSocketMsg
.getChatType())){sendGroupMsg(webSocketMsg
);return;}if(WebSocketMsg
.CHAT_TYPE_EVENT
.equals(webSocketMsg
.getChatType())){if(WebSocketMsg
.MSG_TYPE_REFRESH_FRIEND_LIST
.equals(webSocketMsg
.getMsgType())){refreshFriendList(webSocketMsg
);}return;}}public void sendSingleMsg(WebSocketMsg webSocketMsg
){WebSocket webSocket
= clients
.get(webSocketMsg
.getToId());Friend friend
= null;if(webSocket
!= null){friend
= webSocket
.getFriend(webSocketMsg
.getFromId());}else{List<Friend> myFriendList
= friendService
.findMyFriendList(webSocketMsg
.getToId());for(Friend f
: myFriendList
){if(f
.getFriendAccount().getId().longValue() == webSocketMsg
.getFromId().longValue()){friend
= f
;break;}}}if(friend
== null){webSocketMsg
.setMsgType(WebSocketMsg
.MSG_TYPE_NOTICE
);webSocketMsg
.setToId(userid
);webSocketMsg
.setMsg("你還不是對方的好友,請先加好友!");session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));return;}if(friend
.getStatus() == Friend
.FRIEND_STATUS_BLOCK
){webSocketMsg
.setMsgType(WebSocketMsg
.MSG_TYPE_NOTICE
);webSocketMsg
.setToId(userid
);webSocketMsg
.setMsg("你已被對方拉黑,無法發送消息!");session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));return;}webSocketMsg
.setFromId(friend
.getId());String extAttr
= friend
.getRemark() + ";" + friend
.getFriendAccount().getHeadPic();webSocketMsg
.setExtAttr(extAttr
);if(webSocketMsg
.getMsgType() == WebSocketMsg
.MSG_TYPE_ONLINE
){webSocketMsg
.setMsg("您的好友【" + friend
.getRemark() + "】上線啦!");}if(webSocketMsg
.getMsgType() == WebSocketMsg
.MSG_TYPE_OFFLINE
){webSocketMsg
.setMsg("您的好友【" + friend
.getRemark() + "】下線啦!");}if(webSocket
!= null){webSocket
.session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));}if(webSocketMsg
.getMsgType() == WebSocketMsg
.MSG_TYPE_NOTICE
|| webSocketMsg
.getMsgType() == WebSocketMsg
.MSG_TYPE_ONLINE
|| webSocketMsg
.getMsgType() == WebSocketMsg
.MSG_TYPE_OFFLINE
){return;}saveMsgLog(accountService
.find(webSocketMsg
.getToId()),saveMsgContent(webSocketMsg
));}public void sendGroupMsg(WebSocketMsg webSocketMsg
){List<AccountGroupMember> accountGroupMemberList
= accountGroupMemberService
.findByGroup(webSocketMsg
.getToId());if(accountGroupMemberList
== null){webSocketMsg
.setMsgType(WebSocketMsg
.MSG_TYPE_NOTICE
);webSocketMsg
.setToId(userid
);webSocketMsg
.setMsg("該群已被群主解散!");session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));return;}AccountGroupMember accountGroupMember
= getAccountGroupMember(accountGroupMemberList
,webSocketMsg
.getFromId());if(accountGroupMember
== null){webSocketMsg
.setMsgType(WebSocketMsg
.MSG_TYPE_NOTICE
);webSocketMsg
.setToId(userid
);webSocketMsg
.setMsg("您已被群主移除該群,群成員將不能接受您發送的消息!");session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));return;}String extAttr
= accountGroupMember
.getNickname() + ";" + accountGroupMember
.getMember().getHeadPic();webSocketMsg
.setExtAttr(extAttr
);MsgContent msgContent
= saveMsgContent(webSocketMsg
);for(AccountGroupMember ag
: accountGroupMemberList
){if(ag
.getMember().getId().longValue() != webSocketMsg
.getFromId().longValue()){WebSocket webSocket
= clients
.get(ag
.getMember().getId());if(webSocket
!= null){webSocket
.session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));}saveMsgLog(ag
.getMember(),msgContent
);}}}private Friend getFriend(Long userId
){for(Friend f
: friendList
){if(f
.getFriendAccount().getId().longValue() == userId
.longValue()){return f
;}}return null;}private void loadUnReadMsg(Long userId
){List<MsgLog> msgLogs
= msgLogService
.findByAccountIdAndStatus(userId
, MsgLog
.MSG_STATUS_UNREAD
);for(MsgLog msgLog
: msgLogs
){this.session
.getAsyncRemote().sendText(JSONObject
.toJSONString(msgLog
.getMsgContent()));msgLog
.setStatus(MsgLog
.MSG_STATUS_READ
);msgLogService
.save(msgLog
);}}private AccountGroupMember getAccountGroupMember(List<AccountGroupMember> accountGroupMemberList
,Long id
){for(AccountGroupMember ag
: accountGroupMemberList
){if(ag
.getMember().getId().longValue() == id
.longValue())return ag
;}return null; }private MsgContent saveMsgContent(WebSocketMsg webSocketMsg
){MsgContent msgContent
= new MsgContent();msgContent
.setAttachSize(webSocketMsg
.getAttachSize());msgContent
.setAttachUrl(webSocketMsg
.getAttachUrl());msgContent
.setChatType(webSocketMsg
.getChatType());msgContent
.setExtAttr(webSocketMsg
.getExtAttr());msgContent
.setFromId(webSocketMsg
.getFromId());msgContent
.setMsg(webSocketMsg
.getMsg());msgContent
.setMsgType(webSocketMsg
.getMsgType());msgContent
.setToId(webSocketMsg
.getToId());return msgContentService
.save(msgContent
);}private void saveMsgLog(Account account
,MsgContent msgContent
){MsgLog msgLog
= new MsgLog();msgLog
.setAccount(account
);msgLog
.setMsgContent(msgContent
);WebSocket webSocket
= clients
.get(account
.getId());msgLog
.setStatus(webSocket
== null ? MsgLog
.MSG_STATUS_UNREAD
: MsgLog
.MSG_STATUS_READ
);msgLogService
.save(msgLog
);}public void onlineNotice(Long userid
) {for(Friend friend
: friendList
){WebSocket webSocket
= clients
.get(friend
.getFriendAccount().getId());if(webSocket
!= null){WebSocketMsg webSocketMsg
= new WebSocketMsg();webSocketMsg
.setMsgType(WebSocketMsg
.MSG_TYPE_ONLINE
);webSocketMsg
.setToId(friend
.getFriendAccount().getId());webSocketMsg
.setFromId(userid
);sendSingleMsg(webSocketMsg
);}}}public void offlineNotice(Long userid
) {for(Friend friend
: friendList
){WebSocket webSocket
= clients
.get(friend
.getFriendAccount().getId());if(webSocket
!= null){WebSocketMsg webSocketMsg
= new WebSocketMsg();webSocketMsg
.setMsgType(WebSocketMsg
.MSG_TYPE_OFFLINE
);webSocketMsg
.setToId(friend
.getFriendAccount().getId());webSocketMsg
.setFromId(userid
);sendSingleMsg(webSocketMsg
);}}}public void refreshFriendList(WebSocketMsg webSocketMsg
) {String msg
= webSocketMsg
.getMsg();if(!StringUtils
.isEmpty(msg
)){String[] split
= msg
.split(",");webSocketMsg
.setMsg("您的好友列表有更新啦,快來看看吧!");for(String id
: split
){Long uid
= Long
.valueOf(id
);WebSocket webSocket
= clients
.get(uid
);if(webSocket
!= null){webSocket
.friendList
= friendService
.findMyFriendList(uid
);clients
.put(uid
, webSocket
);webSocket
.session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));}}}friendList
= friendService
.findMyFriendList(userid
);session
.getAsyncRemote().sendText(JSONObject
.toJSONString(webSocketMsg
));}@Autowired
public void setFriendService(FriendService friendService
){WebSocket
.friendService
= friendService
;}@Autowired
public void setMsgContentService(MsgContentService msgContentService
){WebSocket
.msgContentService
= msgContentService
;}@Autowired
public void setMsgLogService(MsgLogService msgLogService
){WebSocket
.msgLogService
= msgLogService
;}@Autowired
public void setAccountGroupMemberService(AccountGroupMemberService accountGroupMemberService
){WebSocket
.accountGroupMemberService
= accountGroupMemberService
;}@Autowired
public void setAccountService(AccountService accountService
){WebSocket
.accountService
= accountService
;}
}
總結
以上是生活随笔為你收集整理的基于Java的实时聊天系统设计与实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。