Background technique
Computer network communication is changing always people's lives as a kind of advanced communication mode.And it establishes
The features such as internet site service on the basis of computer network communication, user's access low by its construction cost facilitates,
As provide information, exchange of information, use information platform.
Social network services, the i.e. website SNS, as a kind of internet site service form, it is intended to people be helped to establish
Social Internet application service, it has also become indispensable a part in daily life.SNS is people-oriented, and emphasis is people
Relationship between people, it is being that tens Internet users construct a true online community.
The development of internet short decades almost revolutionizes the life style of the mankind.Now, conventional internet is being just
It marches toward from the interaction epoch of " people and machine " using SNS as the interaction epoch of " person to person " of representative.The outburst of SNS application represents
The future of internet, as Facebook, Twitter, major telecom operation such as happy net, Tencent, Sina, Sohu, Alibaba
Quotient profoundly recognize the importance of this new Net-volution.
As the developer of SNS application, is all thinking deeply all the time: how can just develop one as Facebook
Could the successful website SNS, how should develop these applications actually attract user? user could be allowed to stay? analyze SNS net
The characteristics of standing, it is seen that real-time interactive between user is allowed to get up and can greatly attract user to come in and play user actively
Come.Using the present invention, user can be allowed to realize real-time interactive on webpage, for example: live chat, comment, notice etc., preferably
Ground enhances interaction, significant increase ground user experience.
It can be using two ways come quasi- using the ajax request/response mode of short connection based on conventional Web2.0 technology
Obtain relevant information in real time: (1) user actively initiates operation requests, after server returned data, using asynchronous load
Mode is shown on webpage;(2) polling request is initiated using browser interval time, after server returned data,
It is shown on webpage by the way of asynchronous load;Family 1 is respectively adopted in above two implementation, user 2 illustrates, such as
Shown in 1 figure.
From 1 figure, it can be seen that be based on routine Web2.0 technology, either existing scheme one or scheme two, be not difficult to send out
Existing, all there are two obvious disadvantages: first is that real-time is not high, second is that can only realize that the data between " user " and " server " pass
Defeated and feedback, can not allow online real-time exchange data between different users, interact, constrain the further of product form
Upgrading, also affects user experience.
Summary of the invention
The technology of the present invention solves the problems, such as: overcoming the deficiencies of the prior art and provide and a kind of realizes real-time, interactive on webpage
System and method can allow user to realize real-time interactive on webpage using the present invention, for example: live chat, comment, notice
Etc., preferably enhance the real-time interactive between user, significant increase ground user experience.
A kind of the technology of the present invention solution: system for realizing real-time, interactive on webpage, characterized by comprising: client
End detection and adaptation module, load balancing module, service status monitoring module, socket registration and locating module, Message Processing
Service module and service logic service module;Wherein:
Client detection and adaptation module: very important module is responsible for carrying out user browser version, characteristic, ip
Location, to script degree of support etc. detection, obtain user input, with message handling services module establish XHR-long poll or
Websocket connection, and receive the functions such as server-side propelling data.The input of the module is keyboard, the mouse action of user
As a result, and by input data carry out JSON said shank, be output to message handling services module;The module takes with Message Processing
Business module is established before XHR-long poll or websocket connection, and long-range adjust can be carried out once with load balancing module
With to obtain more idle message processing module server address.
Load balancing module: realizing the reception of various client messages, according to the busy journey of each message server in rear end
Degree, forwards messages to certain message server of relative free;The input of the module is detected from client and adaptation module,
Query result is returned to client detection and adaptation module by the data for receiving query service condition monitoring module after requesting.Really
The connection of warranty family is evenly distributed in each message handling services module.
Service status monitoring module: the monitoring of the operating condition to background message processing service module, storage load are realized
Data.The data input of the module comes from message handling services module, and the loading condition data of itself are sent service by the latter
Condition monitoring module saves.
Message handling services module: realize with client detection and the XHR-long poll of adaptation module or
Websocket connection service, and realize the grouping of message, it is then sent to corresponding grouping logical server;The module it is defeated
Enter from client detection and adaptation module, after the user data after receiving the coding sent, forwards the data to logical services
Module goes to handle.Meanwhile being established after XHR-long poll or websocket connect with client detection and adaptation module,
It can be by the information registerings such as user ip to socket registration and locating module.
Service logic service module: realizing corresponding business function, for example: carrying out the filtering of keyword, business datum
Calculate, store etc., and by treated, result returns to message server, and returns to client by the interface of load balancing
On.The input of the module is message handling services module, after receiving input data, carries out business logic processing, and generate and want
The propelling data of user, inquiry socket registration and the station address information on locating module are returned to, returned data is sent
Give message handling services module.
The realization process of the client detection and adaptation module:
(1) whether detection browser supports javascript, and user is prompted to allow using javascript;
(2) whether detection browser supports HTML5, if it does not, selecting connection scheme is XHR-long poll, it is no
Then scheme is websocket protocol;
(3) load balancing module is accessed, the address of the message handling services module of current idle is obtained;
(4) agreement (XHR-long poll or websocket) for selecting current browser to support connects idle message
Service module is handled, and by the id of user, the transmissions such as ip address information are gone over;
(5) user input data is obtained, JSON coding is carried out;
(6) user input data is issued to message handling services module.
The realization process of the load balancing module:
(1) backstage can open different messaging service processing modules first for different rooms;
(2) before client detection is connect with adaptation module with message processing module, http request is first sent to load
Balance module inquires an available message processing module address of service in target room;
(3) load balancing module sends inquiry request to service status monitoring module, obtains negative according to incoming room number
Blame the actual address of the message processing module in the room;
(4) address information inquired is returned to client detection and adaptation module by load balancing module.
The realization process of the service status monitoring module:
(1) service status monitoring module may turn on before the starting of all message handling services modules;
(2) all message handling services modules, are all configured with the address of service status monitoring module in configuration file;
(3) message handling services module can will be connected to the client terminal quantity of oneself, and be reported to service shape every 1 minute
Condition monitoring module.
The realization process of the message handling services module:
(1) after the starting of message handling services module, the port information specified in configuration file is first listened to, prepares to receive to come
From the connection request of client detection and adaptation module;
(2) then, the server address where oneself, port numbers are registered to service status prison by message handling services module
Module is controlled, load balancing module is facilitated to go to read;
(3) every 1 minute, the client terminal quantity that message handling services module will also connect itself is packaged into json lattice
The data of formula are sent to service status monitoring module, are saved by the latter;
(4) (request has user id, the address ip after receiving a client detection and the connection request of adaptation module
Information etc.), the socket object that generates is saved in local memory after connection is established, and is facilitated and subsequent is carried out reverse message to it
Push uses;
(5) after socket information preservation to local memory, message handling services module is also by socket information and user
Id information is sent to socket registration and locating module, is saved by the latter.
The realization process of the socket registration and locating module:
(1) dictionary is established in memory after starting;
(2) key of dictionary is user id;
(3) Message Processing that the value of dictionary is connected to by the client detection in user browser with adaptation module
The address of module;
(4) user id, corresponding message processing module address are written the registration request for receiving message handling services module
To dictionary;
(5) inquiry request of service logic service module is received, querying condition is user id, inquires dictionary pair according to id
The value of project is answered to return.
The realization process of the service logic service module:
(1) character string is parsed into the message forwarded;
(2) corresponding logical process is carried out for the content in character string, such as: storage is into database;
(3) to treated, content does some keyword filtrations, is packaged into message, returns to messaging service processing module,
It is transmitted back in corresponding client by load balancing interface.
Method of the invention implements step are as follows:
1, client detection and adaptation module, detect user setting, browser version in user browser, complete connection
The selection of scheme;
2, client detection calls load balancing module with adaptation module, gets idle message handling services module
Address;
3, using the connection scheme determined in step 1, what is got in Connection Step 2 disappears for client detection and adaptation module
Breath processing service module;
4, client detection receives the input of user's keyboard with adaptation module, and data are converted into JSON format, is sent to and disappears
Breath processing service module;
5, message handling services module is by client detection with the socket information of the connection generation of adaptation module, together with hair
The user id information registering brought is registered to socket and locating module;
6, the user input data sent is transmitted to service logic service module and goes to handle by message handling services module;
7, it after service logic service module receives request data, according to the type of message of active user, carries out at logic
Reason (specific processing logic is different according to different business scenarios);
If 8, the business processing is needed to specified user or (these users referred to as push of multiple user's PUSH messages
Target user), then service logic service module sends inquiry request to socket registration and locating module, and inquiry push target is used
The address for the message handling services module that family is connected to;
9, service logic service module sends propelling data request to message handling services module;
10. message handling services module finds the corresponding socket of user id in local memory, propelling data is sent
It goes out;
11. being presented to user after client detection receives propelling data with adaptation module, completing the place of entire real-time messages
Reason.
The present invention is with the advantages of prior art:
(1) it could be completed since traditional real-time, interactive requires to install corresponding plug-in unit in browser, such as flash
Player, ActiveX control etc., what the present invention was realized using the component inside browser, therefore alleviate the peace of user
Dress burden, opening webpage can use.
(2) existing web technology is merely able to realize and request for user, sends the function of response.And it cannot achieve by taking
End be engaged in actively to the function of user browser propelling data.Concrete reason be because http request is short connection, when primary request,
After the completion of response action, which can disconnect automatically, so server-side can not just give user's propelling data again.
(3) in order to solve this problem the present invention, has used 2 important technical solutions, has been described below separately below:
A) it XHR-long poll: when client detection and adapter assembly discovery user browser older version, does not support
When HTML5 technology, this scheme can be selected.In the program, client detection, which can first send the http that shakes hands with adapter assembly, is asked
Message handling services module is sought, generates a sessionID, and arrange a poll time in the past T.Then, client is examined
Survey and adapter assembly can send 1 common http request to message handling services module, wait its response.And Message Processing takes
It after business module receives this http request, will not immediately return to, but sessionID, initiation time are recorded, and set
A fixed timer, triggered time T: before the timer expires, if there is data need to be pushed to the user, then takes out
The request object come is left before, sends response message to it, this http link at this time disconnects.And browser receives the response
Afterwards, above-mentioned movement is repeated at once, is sent a http request again to message handling services module, is waited push next time.
B) websocket: when client detection supports HTML5 technology with adapter assembly discovery user browser version,
This scheme can be selected.This scheme is one kind of browser support of major browser manufacturer in latest edition in browser running background
The technology of socket.The interface that the present invention provides different browsers is encapsulated, and a kind of unified data format is provided,
Websocket connection can successfully be established by realizing under different browsers.
(4) since http is stateless, short connection, so must be initiated by client using modes such as ajax, one
As complete the readings of data using the mode of the poll inquiry of client, the present invention using the long connection in the page side
Formula, server and client keep long connection, and server can have submitted interactive real-time with active push data to user.
Specific embodiment
As shown in Fig. 2, method of the invention implements step are as follows:
1, client detection and adaptation module, detect user setting, browser version in user browser, complete connection
The selection of scheme;
2, client detection calls load balancing module by http request with adaptation module, attempts to obtain idle message
Handle the address of service module:
A) format is requested are as follows: http://domainName/getFreeHostAddress;
B) after load balancing module receives request, the data stored in query service condition monitoring module, field includes: clothes
Business device address, current on-line joining process number;
C) load balancing module in memory arranges the record inquired according to " current on-line joining process number " from small to large,
The least server address of taking-up number;
D) address is returned to;
3, using the connection scheme determined in step 1, what is got in Connection Step 2 disappears for client detection and adaptation module
Breath processing service module;
4, client detection receives the input of user's keyboard with adaptation module, and data are converted into JSON format, is sent to and disappears
Breath processing service module;
5, message handling services module is by client detection with the socket information of the connection generation of adaptation module, together with hair
The user id information registering brought is registered to socket and locating module;
A) message handling services module send registration request to socket register and locating module, data format:
{ userID:<long>, socketid:<character>};
B) socket register and locating module with userID as key, socketid value is written to local memory
In dictionary;
C) it succeeds in registration, returns.
6, the user input data sent is transmitted to service logic service module and goes to handle by message handling services module;
7, it after service logic service module receives request data, according to the type of message of active user, carries out at logic
Reason (specific processing logic is different according to different business scenarios);
If 8, the business processing is needed to specified user or (these users referred to as push of multiple user's PUSH messages
Target user), then service logic service module sends inquiry request to socket registration and locating module, and inquiry push target is used
The address for the message handling services module that family is connected to;
9, service logic service module sends propelling data request to message handling services module;
10, message handling services module finds the corresponding socket of user id in local memory, and propelling data is sent
It goes out;
11, after client detection receives propelling data with adaptation module, it is presented to user, completes the place of entire real-time messages
Reason.
As shown in figure 4, load balancing module is implemented as follows:
The main solution of load balancing module:
A) when user volume is bigger, a server is no longer satisfied demand, needs support multiserver point
Cloth deployment;
B) since webSocket connection belongs to long connection, the different requests of a link have context relation, so not
Load balancing simply can be carried out using existing http reverse proxy mode, need independent research load balancing module.
The main strategy for realizing load is:
1, different messaging service components is opened for different rooms;
2, when client request connects, load balancing interface, the available clothes of one of inquiry target room are first called
Business address;
3, load balancing component internal maintains the load information of different server, when receiving scheduling request, looks into
Most idle server address is ask to return.
As shown in figure 3, message handling services module is implemented as follows:
Message handling services module mainly uses webSocket to service, and mainly solves:
1, for realizing the long connection message communication based on browser, so that server-side and client energy real time communication;
2, user can be allowed in the case where not installing any plug-in unit, realizes and receives and dispatches, mentions with the real-time messages of server end
The timeliness of high message.
The realization process of message handling services module:
1, the version information of browser and the connection performance of support are obtained in the message that client establishes connection;
2, XHR-Polling component or websocket-client component are loaded according to the characteristic of browser;
3, websocket server is connected to using one of step 2 mode, and corresponding message subscribing processing is set
Function;
4, message is received, circulation is handled.