基于WebSocket协议的在线聊天系统设计与实现
发布时间:2018-03-01 12:08
本文关键词: WebSocket Session共享 集群 消息转发 出处:《东南大学》2016年硕士论文 论文类型:学位论文
【摘要】:随着电子商务的发展,各大电商平台发展迅速,网络购物逐渐成为人们主要消费的方式之一。在网络购物过程中,通过聊天软件与商家交流是用户获取信息最直接、有效的途径。目前,各大电商平台中的聊天系统多是采用基于HTTP协议的轮询、长轮询等技术来实现的,为了及时获取新信息,浏览器端需频繁的向服务端发送HTTP请求,且每次交互需要携带几百字节的协议头信息。另外,由于消息在服务端更新时间不确定,在交互过程中,很大比例请求不能获取到新消息。WebSocket协议的出现为解决上述问题提供了途径。WebSocket协议为服务器、浏览器间定义了一种全双工、异步、单套接字连接,能够实现服务器浏览器间异步、双工通信,且在通信过程中WebSocket协议头最多占用14字节,非常高效。如能基于WebSocket协议实现聊天软件,则可以有效解决上述问题。然而,将WebSocket协议应用于在线聊天系统时,面临三个主要困难,即维持WebSocket连接、集群中Session共享和服务端消息处理。WebSocket连接维持困难指的是多种因素可能会导致浏览器与服务器间的连接失效;集群中Session共享困难指的是Session实例保存在不同服务器实例的内存中,难以共享;服务端处理消息困难指的是用户所有的消息都需要经过服务端处理、转发,处理压力大。本文针对上述三个问题进行深入研究,在详细分析问题存在的根本原因后,设计了特定的模块来解决上述问题:WebSocket连接保持模块借鉴保持Socket持久连接的设计思路,通过心跳连接、失效重连、连接监测等机制减少WebSocket连接失效情况;Session共享模块通过Nginx报务器和Memcached缓存服务器来解决Session共享问题,其中,配置了Ip_hash策略的Nginx服务器用于均衡请求,Memcached服务器用于记录集群Session分布情况;服务端消息处理模块采用消息确认、重发、补偿和保存消息到数据库等多种方式,保证服务端处理消息的准确性。在此基础上,最终实现了基于WebSocket协议的在线聊天系统,并通过实验表明该系统有效地克服了三个主要难点,解决了现有聊天系统中存在的服务器无法主动推送、服务器浏览器间存在大量无效交互、请求协议头占用大量带宽等问题。另外,本文论证了将WebSocket应用于对实时性要求严格系统的可能性,并为以后将WebSocket协议应用于其他系统提供一定帮助。
[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.
【学位授予单位】:东南大学
【学位级别】:硕士
【学位授予年份】:2016
【分类号】:TP311.52
【参考文献】
相关硕士学位论文 前10条
1 石文涛;Html5中WebSocket协议关键技术的研究及基于WebSocket协议的实时Web通信系统的实现[D];南京邮电大学;2014年
2 姚小勇;基于Memcached的中国移动CRM系统性能分析及优化[D];中山大学;2013年
3 廖清平;基于Web的服务器的Push技术研究和应用[D];中山大学;2013年
4 李兴华;基于WebSocket的移动即时通信系统[D];重庆大学;2013年
5 冼学辉;基于Web的实时信息推送技术的研究[D];华北电力大学;2013年
6 戴华;基于Nginx和Memcached的高并发WEB服务器设计[D];复旦大学;2013年
7 吴欣铠;结合memcached技术的社交游戏《人人城市》的设计与实现[D];南京大学;2012年
8 邓世权;基于Memcached的高速缓存功能扩展研究[D];西南交通大学;2012年
9 杨文婷;基于HTTP长连接的消息推送平台的研究与实现[D];华中科技大学;2012年
10 兰翔;基于Nginx的负载均衡技术的研究与改进[D];华南理工大学;2012年
,本文编号:1551852
本文链接:https://www.wllwen.com/jingjilunwen/dianzishangwulunwen/1551852.html