Invention content
In view of the defects existing in the prior art, the present invention provides a kind of real-time messages method for pushing based on Socket technologies
And system, it can effectively solve the above problems.
The technical solution adopted by the present invention is as follows:
The present invention provides a kind of real-time messages method for pushing based on Socket technologies, includes the following steps:
Step 1, the total access number of website active user is obtained in real time, according to the total access number of the active user, adjustment
The quantity of data-pushing module, it is assumed that the configuration quantity of data-pushing module is N number of, is denoted as respectively:1st data-pushing module,
2nd data-pushing module ... Nth data pushing module;Wherein, N is natural number;
Step 2, each data-pushing module configures a corresponding data distribution module and a buffer queue B, i.e.,:
For arbitrary i-th data-pushing module, the i-th data distribution module and the i-th buffer queue B are configured;Wherein, i=1,
2...N;Therefore, it is configured with N number of data distribution module and N number of buffer queue B altogether;
Step 3, current each access user is assigned to each by agent transmitting module based on presetting allocation rule
In the room of data-pushing module management, and each current accessed user is made to establish Socket long connections with website service end;
Step 4, the comment information that Data write. module real-time collecting user is submitted by the Form lists in WEB page,
And the comment information is transferred to data security module;
Step 5, the data security module carries out safety filtering to the comment information, if obtaining legal comment letter
Then specified data library is written in the legal comment information by breath, and successfully logical to Data write. module transmission write-in
Know message;
Step 6, it after the Data write. module receives the successful notification message of write-in, legal is commented what step 5 obtained
It is written in buffer queue A by information;
Step 7, N number of data distribution module polls read the legal comment information that buffer queue A is newly written, for each the
The legal comment information read is written after the buffer queue A reads legal comment information for i data distribution modules
Into corresponding i-th buffer queue B;Then, the i-th data-pushing module reads legal comment from corresponding i-th buffer queue B
By information, then each of room for judging the i-th data-pushing module management access user enter room time whether earlier than
I-th data-pushing module reads the time of legal comment information from corresponding i-th buffer queue B, if earlier than described
The legal comment information is just pushed to access user by i data-pushings module;Otherwise, the i-th data-pushing module is not
The legal comment information can be pushed to access user.
Preferably, in step 1, the quantity of data-pushing module is adjusted, specifically, always accessing number according to the active user
Amount, increases or decreases the quantity of data-pushing module, specifically includes:
Step 1.1, a data pushing module is increased or decreased if necessary, then changes and join in data-pushing configuration file
Number, increases or decreases a data-pushing port;
Step 1.2, restart website;
Step 1.3, the data-pushing configuration file is read, create or deletes data-pushing module, thus increases and adds deduct
Minority is according to pushing module.
Preferably, in step 3, agent transmitting module is based on presetting allocation rule, by current each access user
It is assigned in the room of each data-pushing module management, specially:
Step 3.1, agent transmitting module accesses source according to user, obtains and accesses User IP information;
Step 3.2, agent transmitting module will access user and be assigned to corresponding data-pushing module tubes according to hash algorithm
In the room of reason, and access user is made to establish Socket long connections with website service end;
Step 3.3, if step 3.2 executes failure, user will be accessed and be assigned to response time shortest data-pushing
In the room of module management.
Preferably, in step 3, further include:
Room user management module, for moving the user of low liveness in the room of data-pushing module management
Go out, and then ensures in room that new access user can rapidly enter data-pushing module management;It specifically includes:
1) room user management module reads the room configuration file and real-time condition in the room of data-pushing module management;
2) whether carrying number in room user management module reading room, which is higher than in configuration file, is appraised and decided carrying number;If
It is no, without processing;If so, to the user in room according to first in first out and minimum speech mechanism, by a certain proportion of use
Family is removed;Wherein, specific ratio value is specified in room configuration file.
Preferably, in step 5, the data security module carries out safety filtering to the comment information, specially:
Security filtering index is arranged in the data security module;The data security module is according to the security filtering index
Data filtering processing is carried out to the comment information, including:SQL filtration treatments, filtering sensitive words processing and advertisement filter processing,
If obtaining legal comment information, the legal comment information is returned;If not obtaining legal comment information, institute's commentary
By information less than the security level formulated, to submitting the access user of the comment information to carry out prohibiting locking processing of making peace.
Preferably, in step 7, the i-th data-pushing module reads legal comment letter from corresponding i-th buffer queue B
Breath, specially:
The data lifetime of i-th buffer queue B is T;The i-th data-pushing module reads the i-th buffer queue B's
Caching interval time is read less than data lifetime T.
The present invention also provides a kind of real-time messages supplying systems based on Socket technologies, including:
Data write. module, for the comment information that real-time collecting user is submitted by the Form lists in WEB page, and
The comment information is transferred to data security module;
Data security module, the comment information for the Data write. module to be written carry out safety filtering, such as
Fruit obtains legal comment information, then is sent to the Data write. module and successful notification message is written;By Data write. module
Legal comment information is written in buffer queue A;
Data distribution module is N number of, and each data distribution module polls read the legal comment that buffer queue A is newly written
The legal comment information read is written in corresponding buffer queue B by information;Wherein, the quantity of buffer queue B is N number of,
One buffer queue B uniquely corresponds to a data distribution module;
Data-pushing module is N number of, and a data pushing module uniquely corresponds to a buffer queue B, is used for from corresponding
Legal comment information is read in buffer queue B, then each of room for judging data-pushing module self-management is accessed and used
Family enter room time whether read from corresponding buffer queue B earlier than data-pushing module legal comment information when
Between, if earlier than the legal comment information is just pushed to access user by the data-pushing module;
Agent transmitting module, for based on presetting allocation rule, current each access user being assigned to each
In the room of data-pushing module management, and each current accessed user is made to establish Socket long connections with website service end.
Preferably, further include:
Room user management module, for moving the user of low liveness in the room of data-pushing module management
Go out, and then ensures in room that new access user can rapidly enter data-pushing module management.
A kind of real-time messages method for pushing and system based on Socket technologies provided by the invention have the following advantages:
In conventional web, the comment information that some user delivers efficiently can quickly be presented to other current accesseds
User increases usage rate of the user so as to make online friend carry out efficient interaction in conventional web, and then improves website and visit
The amount of asking.
Specific implementation mode
In order to make the technical problems, technical solutions and beneficial effects solved by the present invention be more clearly understood, below in conjunction with
Accompanying drawings and embodiments, the present invention will be described in further detail.It should be appreciated that specific embodiment described herein only to
It explains the present invention, is not intended to limit the present invention.
A kind of real-time messages method for pushing and system based on Socket technologies provided by the invention, can allow online friend passing
Efficient interaction is carried out on system webpage, increases usage rate of the user and is combined using Node.js to improve site volume
WebSocket technologies in Html5 realize the function.
Real-time messages method for pushing and system based on Socket technologies include mainly Data write. module, data safety
Module, data distribution module, data-pushing module, agent transmitting module, room user management module totally six modules.Data are write
Enter module to need to be used together in conjunction with data security module, the content of Data write. module output is as the defeated of data distribution module
Enter information, the data of the data distribution module distribution input information as data-pushing module again, data-pushing module be responsible for by
Data-pushing gives different users, realizes the effect of instant messaging.And agent transmitting module is responsible for assigning the user to difference
The room that is subordinate to of data-pushing module, and room user management module is the overall technology in order to ensure that high viscosity user is online
Frame is referring to attached drawing 1.
Real-time messages method for pushing based on Socket technologies, includes the following steps:
Step 1, the total access number of website active user is obtained in real time, according to the total access number of the active user, adjustment
The quantity of data-pushing module, it is assumed that the configuration quantity of data-pushing module is N number of, is denoted as respectively:1st data-pushing module,
2nd data-pushing module ... Nth data pushing module;Wherein, N is natural number;
In this step, the quantity of data-pushing module is adjusted, specifically, according to the total access number of the active user, is increased
Add deduct a small number of quantity according to pushing module, specifically includes:
Step 1.1, a data pushing module is increased or decreased if necessary, then changes and join in data-pushing configuration file
Number, increases or decreases a data-pushing port;
Step 1.2, restart website;
Step 1.3, the data-pushing configuration file is read, create or deletes data-pushing module, thus increases and adds deduct
Minority is according to pushing module.
Step 2, each data-pushing module configures a corresponding data distribution module and a buffer queue B, i.e.,:
For arbitrary i-th data-pushing module, the i-th data distribution module and the i-th buffer queue B are configured;Wherein, i=1,
2...N;Therefore, it is configured with N number of data distribution module and N number of buffer queue B altogether;
Step 3, current each access user is assigned to each by agent transmitting module based on presetting allocation rule
In the room of data-pushing module management, and each current accessed user is made to establish Socket long connections with website service end;
In this step, current each access user is assigned to by agent transmitting module based on presetting allocation rule
In the room of each data-pushing module management, specially:
Step 3.1, agent transmitting module accesses source according to user, obtains and accesses User IP information;
Step 3.2, agent transmitting module will access user and be assigned to corresponding data-pushing module tubes according to hash algorithm
In the room of reason, and access user is made to establish Socket long connections with website service end;
Step 3.3, if step 3.2 executes failure, user will be accessed and be assigned to response time shortest data-pushing
In the room of module management.
In step 3, further include:
Room user management module, for moving the user of low liveness in the room of data-pushing module management
Go out, and then ensures in room that new access user can rapidly enter data-pushing module management;It specifically includes:
1) room user management module reads the room configuration file and real-time condition in the room of data-pushing module management;
2) whether carrying number in room user management module reading room, which is higher than in configuration file, is appraised and decided carrying number;If
It is no, without processing;If so, to the user in room according to first in first out and minimum speech mechanism, by a certain proportion of use
Family is removed;Wherein, specific ratio value is specified in room configuration file.
Step 4, the comment information that Data write. module real-time collecting user is submitted by the Form lists in WEB page,
And the comment information is transferred to data security module;
Step 5, the data security module carries out safety filtering to the comment information, if obtaining legal comment letter
Then specified data library is written in the legal comment information by breath, and successfully logical to Data write. module transmission write-in
Know message;
In step 5, the data security module carries out safety filtering to the comment information, specially:
Security filtering index is arranged in the data security module;The data security module is according to the security filtering index
Data filtering processing is carried out to the comment information, including:SQL filtration treatments, filtering sensitive words processing and advertisement filter processing,
If obtaining legal comment information, the legal comment information is returned;If not obtaining legal comment information, institute's commentary
By information less than the security level formulated, to submitting the access user of the comment information to carry out prohibiting locking processing of making peace.
Step 6, it after the Data write. module receives the successful notification message of write-in, legal is commented what step 5 obtained
It is written in buffer queue A by information;
Step 7, N number of data distribution module polls read the legal comment information that buffer queue A is newly written, for each the
The legal comment information read is written after the buffer queue A reads legal comment information for i data distribution modules
Into corresponding i-th buffer queue B;Then, the i-th data-pushing module reads legal comment from corresponding i-th buffer queue B
By information, then each of room for judging the i-th data-pushing module management access user enter room time whether earlier than
I-th data-pushing module reads the time of legal comment information from corresponding i-th buffer queue B, if earlier than described
The legal comment information is just pushed to access user by i data-pushings module;Otherwise, the i-th data-pushing module is not
The legal comment information can be pushed to access user.
In step 7, the i-th data-pushing module reads legal comment information from corresponding i-th buffer queue B, specifically
For:
The data lifetime of i-th buffer queue B is T;The i-th data-pushing module reads the i-th buffer queue B's
Caching interval time is read less than data lifetime T.
The present invention also provides a kind of real-time messages supplying systems based on Socket technologies, include mainly that mould is written in data
Block, data security module, data distribution module, data-pushing module, agent transmitting module, room user management module totally six
Module.Each module is discussed in detail respectively below:
(1) Data write. module
Data write. module, for the comment information that real-time collecting user is submitted by the Form lists in WEB page, and
The comment information is transferred to data security module;After pending data security module carries out safety filtering processing to data, then will
Legal comment information is written in buffer queue A;
Data write. module is essential module, and the flow of specific implementation is as shown in Figure 2:
1) comment information input by user is collected by the Form lists in WEB page;
2) necessary safety filtering (being handled by data security module) is carried out to the comment information of input;
3) filtered legal comment information write-in specified data library;
4) after being written successfully in above-mentioned 3, legal comment information is written in buffer queue A;Otherwise it is not written into;
5) user socket is established with website service end by agent transmitting module to connect.
(2) data security module
Data security module is used to carry out safety filtering, such as mistake to the comment information that the Data write. module is written
Filter junk data, malicious code data etc..If obtaining legal comment information, it is written to Data write. module transmission
The notification message of work(;Legal comment information is written in buffer queue A by Data write. module.
Data security module is essential module, and the thinking of specific implementation is with reference to figure 3:
1) pending comment information and security filtering index are read;
2) according to security filtering index to comment information filtration treatment, such as filtering insult character, SQL injection code
Deng;
3) according to the security level of formulation, while to submitting the user of such comment information to carry out the processing such as taboo speech, locking;
4) the legal comment information in request end is returned to.
(3) data distribution module
Data distribution module is N number of, and N is random natural number, can be 1, or any amount.Each data
Distribution module reads the legal comment information that buffer queue A is newly written for poll, and the legal comment information read is written
Into corresponding buffer queue B;Wherein, the quantity of buffer queue B is N number of, and a buffer queue B uniquely divide by a corresponding data
Send out module.
Data distribution module is essential module, and the thinking of specific implementation is with reference to figure 4:
1) data distribution module can have N number of, and particular number is adjusted according to the size of user volume, and each module undertakes
User volume is advisable with being no more than 3000;
2) data distribution module polls read buffer queue A;
3) data read are written in buffer queue B by data distribution module, and buffer queue B may have N number of, number
Amount is consistent with data distribution number of modules;
4) data lifetime in buffer queue B is usually no more than 300 seconds, and data-pushing module reads caching interval time
It is necessarily less than the value.
(4) data-pushing module
Data-pushing module is N number of, is consistent with B number of above-mentioned buffer queue, a data pushing module is uniquely right
A buffer queue B is answered, for reading legal comment information from corresponding buffer queue B, then judges data-pushing module
Each of room of self-management access user enter room time whether earlier than data-pushing module from corresponding caching
The time of legal comment information is read in queue B, if earlier than the data-pushing module is just by the legal comment information
It is pushed to access user;
Data-pushing module is essential module, and the thinking of specific implementation is with reference to figure 5:
1) data-pushing module can have N number of, and particular number is adjusted according to the size of user volume, each data-pushing
Module undertakes user volume to be advisable with being no more than 3000;
2) each data-pushing module polls read corresponding buffer queue B;
3) data-pushing of the buffer queue B read is given the user being each responsible for, these users by data-pushing module
It is that long connection (being based on websocket protocol) is kept by socket with Website server.
(5) agent transmitting module
Agent transmitting module, for based on presetting allocation rule, current each access user being assigned to each
In the room of data-pushing module management, and each current accessed user is made to establish Socket long connections with website service end.
Agent transmitting module is essential module, and specific implementation thinking is with reference to figure 6:
1) source is accessed according to user, agent transmitting module obtains User IP relevant information;
2) agent transmitting module calculates user according to HASH algorithms and is attached with X data pushing module;
If 3) connection failure in above-mentioned steps 2, the Response to selection time, shortest data-pushing module was attached;
4) user establishes Socket with server-side and connect.
(6) room user management module
Room user management module, for moving the user of low liveness in the room of data-pushing module management
Go out, and then ensures in room that new access user can rapidly enter data-pushing module management.
Room user management module is optional module, but suggests using the module, the thinking reference chart of specific implementation
7:
1) configuration file and real-time condition in corresponding room, are read;
2), room user management module judges room carrying number whether less than carrying number is appraised and decided in configuration file, such as
Fruit is less than, without processing;
3) after, being more than, then to the user in the room according to first in first out (FIFO) and minimum speech (LRU) both machines
System removes a certain proportion of user (ratio is specified in configuration file).
Real-time messages supplying system provided by the invention based on Socket technologies, the core of the system are pushed away in data
Module, such design is sent to be very suitable for increasing or decreasing come correspondingly dilatation or contraction according to user volume in the future.Data
Each host process is all independently operated in pushing module, is not necessarily to communication from each other.
The invention will be further described by taking the comment of articles page real time push as an example below.
Corresponding scripted code is added in Website page, which can be carried when user to access pages to allow
Rise agreement (supporting websocket);
When user A accesses the Website page, and have submitted comment information;
Server-side receives comment information, and the comment information is sent to data security module first;
Data security module carries out the comment information specified cleaning according to the needs of business, uses in the method
Sql is filtered and filtering sensitive words this 2 functions are if detect at this 2 points can also carry out the operations such as taboo speech, locking to user;
Safe data are obtained after filtering can just be written into database, can be straight if not having any data after filtering
Return is connect, reminds user that input is illegal;
After being written successfully, then write the data in buffer queue A;
Data in buffer queue A can be read again, is written in multiple buffer queue B, this ensures that often
A data pushing module reads identical data;
Data-pushing module reads respective buffer queue B, the use being then sent to the information read in being connected thereto
Family;
So far, other users that the comment information that user A is sent will be browsed the website view.
A kind of real-time messages method for pushing and system based on Socket technologies provided by the invention have the following advantages:
In conventional web, the comment information that some user delivers efficiently can quickly be presented to other current accesseds
User increases usage rate of the user so as to make online friend carry out efficient interaction in conventional web, and then improves website and visit
The amount of asking.
The above is only a preferred embodiment of the present invention, it is noted that for the ordinary skill people of the art
For member, various improvements and modifications may be made without departing from the principle of the present invention, these improvements and modifications are also answered
Depending on protection scope of the present invention.