基于WebSocket協(xié)議的在線聊天系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)
本文關(guān)鍵詞: WebSocket Session共享 集群 消息轉(zhuǎn)發(fā) 出處:《東南大學(xué)》2016年碩士論文 論文類型:學(xué)位論文
【摘要】:隨著電子商務(wù)的發(fā)展,各大電商平臺(tái)發(fā)展迅速,網(wǎng)絡(luò)購(gòu)物逐漸成為人們主要消費(fèi)的方式之一。在網(wǎng)絡(luò)購(gòu)物過程中,通過聊天軟件與商家交流是用戶獲取信息最直接、有效的途徑。目前,各大電商平臺(tái)中的聊天系統(tǒng)多是采用基于HTTP協(xié)議的輪詢、長(zhǎng)輪詢等技術(shù)來(lái)實(shí)現(xiàn)的,為了及時(shí)獲取新信息,瀏覽器端需頻繁的向服務(wù)端發(fā)送HTTP請(qǐng)求,且每次交互需要攜帶幾百字節(jié)的協(xié)議頭信息。另外,由于消息在服務(wù)端更新時(shí)間不確定,在交互過程中,很大比例請(qǐng)求不能獲取到新消息。WebSocket協(xié)議的出現(xiàn)為解決上述問題提供了途徑。WebSocket協(xié)議為服務(wù)器、瀏覽器間定義了一種全雙工、異步、單套接字連接,能夠?qū)崿F(xiàn)服務(wù)器瀏覽器間異步、雙工通信,且在通信過程中WebSocket協(xié)議頭最多占用14字節(jié),非常高效。如能基于WebSocket協(xié)議實(shí)現(xiàn)聊天軟件,則可以有效解決上述問題。然而,將WebSocket協(xié)議應(yīng)用于在線聊天系統(tǒng)時(shí),面臨三個(gè)主要困難,即維持WebSocket連接、集群中Session共享和服務(wù)端消息處理。WebSocket連接維持困難指的是多種因素可能會(huì)導(dǎo)致瀏覽器與服務(wù)器間的連接失效;集群中Session共享困難指的是Session實(shí)例保存在不同服務(wù)器實(shí)例的內(nèi)存中,難以共享;服務(wù)端處理消息困難指的是用戶所有的消息都需要經(jīng)過服務(wù)端處理、轉(zhuǎn)發(fā),處理壓力大。本文針對(duì)上述三個(gè)問題進(jìn)行深入研究,在詳細(xì)分析問題存在的根本原因后,設(shè)計(jì)了特定的模塊來(lái)解決上述問題:WebSocket連接保持模塊借鑒保持Socket持久連接的設(shè)計(jì)思路,通過心跳連接、失效重連、連接監(jiān)測(cè)等機(jī)制減少WebSocket連接失效情況;Session共享模塊通過Nginx報(bào)務(wù)器和Memcached緩存服務(wù)器來(lái)解決Session共享問題,其中,配置了Ip_hash策略的Nginx服務(wù)器用于均衡請(qǐng)求,Memcached服務(wù)器用于記錄集群Session分布情況;服務(wù)端消息處理模塊采用消息確認(rèn)、重發(fā)、補(bǔ)償和保存消息到數(shù)據(jù)庫(kù)等多種方式,保證服務(wù)端處理消息的準(zhǔn)確性。在此基礎(chǔ)上,最終實(shí)現(xiàn)了基于WebSocket協(xié)議的在線聊天系統(tǒng),并通過實(shí)驗(yàn)表明該系統(tǒng)有效地克服了三個(gè)主要難點(diǎn),解決了現(xiàn)有聊天系統(tǒng)中存在的服務(wù)器無(wú)法主動(dòng)推送、服務(wù)器瀏覽器間存在大量無(wú)效交互、請(qǐng)求協(xié)議頭占用大量帶寬等問題。另外,本文論證了將WebSocket應(yīng)用于對(duì)實(shí)時(shí)性要求嚴(yán)格系統(tǒng)的可能性,并為以后將WebSocket協(xié)議應(yīng)用于其他系統(tǒng)提供一定幫助。
[Abstract]:With the development of electronic commerce and the rapid development of various e-commerce platforms, online shopping has gradually become one of the main ways for people to consume. In the process of online shopping, it is the most direct way for users to obtain information through chat software. At present, most of the chat systems in the major e-commerce platforms are implemented by polling and long polling based on HTTP protocol. In order to get new information in time, the browser needs to send HTTP requests to the server frequently. And each interaction needs to carry hundreds of bytes of protocol header information. A large proportion of requests can not get new message. WebSocket protocol provides a way to solve the above problem. WebSocket protocol defines a full duplex, asynchronous, single socket connection between browsers, which can realize asynchronous between server browsers. Duplex communication, and in the process of communication, the WebSocket protocol header takes up to 14 bytes, which is very efficient. If we can implement chat software based on WebSocket protocol, we can solve the above problem effectively. However, when we apply WebSocket protocol to online chat system, Facing three main difficulties, namely, maintaining WebSocket connection, Session sharing in cluster and message processing on server side. WebSocket connection maintenance difficulty refers to a variety of factors that may lead to the failure of the connection between browser and server. The difficulty of Session sharing in cluster is that the Session instance is stored in the memory of different server instances, so it is difficult to share, and the difficulty of processing messages on the server side means that all the messages of the user need to be processed and forwarded by the server side. In this paper, the three problems mentioned above are studied in depth. After analyzing the root causes of the problems in detail, a specific module is designed to solve the above problems. Through heartbeat connection, failure reconnection, connection monitoring and other mechanisms to reduce the failure of WebSocket connection the session sharing module solves the problem of Session sharing through Nginx server and Memcached cache server. The Nginx server which is configured with Ip_hash policy is used to balance the request of Nginx server to record the distribution of cluster Session, and the message processing module on the server side adopts message confirmation, retransmission, compensation and saving messages to database, and other methods, such as message confirmation, retransmission, compensation and saving message to database, etc. On the basis of this, an online chat system based on WebSocket protocol is implemented, and the experiments show that the system overcomes three main difficulties effectively. It solves the problems that the server in the existing chat system can't push actively, the server browser has a lot of invalid interaction, the request protocol header takes up a lot of bandwidth, and so on. This paper demonstrates the possibility of applying WebSocket to the system with strict real-time requirements, and provides some help for the application of WebSocket protocol to other systems in the future.
【學(xué)位授予單位】:東南大學(xué)
【學(xué)位級(jí)別】:碩士
【學(xué)位授予年份】:2016
【分類號(hào)】:TP311.52
【參考文獻(xiàn)】
相關(guān)碩士學(xué)位論文 前10條
1 石文濤;Html5中WebSocket協(xié)議關(guān)鍵技術(shù)的研究及基于WebSocket協(xié)議的實(shí)時(shí)Web通信系統(tǒng)的實(shí)現(xiàn)[D];南京郵電大學(xué);2014年
2 姚小勇;基于Memcached的中國(guó)移動(dòng)CRM系統(tǒng)性能分析及優(yōu)化[D];中山大學(xué);2013年
3 廖清平;基于Web的服務(wù)器的Push技術(shù)研究和應(yīng)用[D];中山大學(xué);2013年
4 李興華;基于WebSocket的移動(dòng)即時(shí)通信系統(tǒng)[D];重慶大學(xué);2013年
5 冼學(xué)輝;基于Web的實(shí)時(shí)信息推送技術(shù)的研究[D];華北電力大學(xué);2013年
6 戴華;基于Nginx和Memcached的高并發(fā)WEB服務(wù)器設(shè)計(jì)[D];復(fù)旦大學(xué);2013年
7 吳欣鎧;結(jié)合memcached技術(shù)的社交游戲《人人城市》的設(shè)計(jì)與實(shí)現(xiàn)[D];南京大學(xué);2012年
8 鄧世權(quán);基于Memcached的高速緩存功能擴(kuò)展研究[D];西南交通大學(xué);2012年
9 楊文婷;基于HTTP長(zhǎng)連接的消息推送平臺(tái)的研究與實(shí)現(xiàn)[D];華中科技大學(xué);2012年
10 蘭翔;基于Nginx的負(fù)載均衡技術(shù)的研究與改進(jìn)[D];華南理工大學(xué);2012年
,本文編號(hào):1551852
本文鏈接:http://sikaile.net/jingjilunwen/dianzishangwulunwen/1551852.html