Node.js Chat Program How to implement AJAX long-polling Long link refresh mode _javascript skills

Source: Internet
Author: User
Tags message queue setinterval
Nonsense don't say much, start today's theme. Looking at this program, the most valuable thing is to show how to use Nodejs to implement long link mode refresh technology.
(This program is not detailed, focus on this feature)
Client.js
First look at a piece of core code:
Copy Code code as follows:

function Longpoll (data) {
//.... * * Line omitted here
$.ajax ({Cache:false
, type: "Get"
, url: "/recv"
, DataType: "JSON"
, data: {since:CONFIG.last_message_time, id:CONFIG.id}
, Error:function () {
AddMessage ("", "long poll error.) Trying again ... ", New Date ()," error ");
Transmission_errors + 1;
Don ' t flood the servers on error, wait seconds before retrying
SetTimeout (Longpoll, 10*1000);
}
, Success:function (data) {
transmission_errors = 0;
If everything went, begin another request immediately
The server would take a long time to respond
How long? So, it'll wait until there are another message
And then it'll return it to us and close the connection.
Since the connection is closed when we get data, we longpoll again
Longpoll (data);
}
});
}

This is a section of code in the Client.js, a look at this code, we should immediately think of two words-"recursion." In the Longpoll method, call the Longpoll method again, a typical recursive call.
Based on the semantics of this code, it can be seen that the first time it is loaded, the Longpoll method is invoked, the value is fetched asynchronously to "/RESV", and if successful, the success method is executed and the Longpoll method is immediately called again. If it fails, the error function is executed, and the Longpoll method is called again in 10 seconds. Of course, the implementation of the error method has a certain number of restrictions, by variable TRANSMISSION_ERRORSX control.
You may have a question, so that the recursive loop to get the data, the server will have a great burden? Is it always the same when there is no data to be obtained? Of course, the answer is negative! And, Nodejs take advantage of their own characteristics, very good deal with this problem. Then look down:
Server.js
Now see how the server responds to the above client invocation, the core code:
Copy Code code as follows:

Fu.get ("/recv", Function (req, res) {
validation and updating of Session ...
Channel.query (since, function (messages) {
if (session) Session.poke ();
Res.simplejson ({messages:messages, rss:mem.rss});
});
});

Let's not care what this fu.get () means, it has nothing to do with this tutorial. It's OK to know that it can respond to client calls. The code above, in addition to some of the session's operations, just called the Channel Query method. Note the parameters passed:
Since, it recorded a time;
anonymous method, which takes a messages parameter, two actions: 1 update session time, 2 return a JSON, that is, return messages to the client.
One might have a question: Why not just return to messages here? Why do I have to define a method in a channel? Answer: If so, it becomes a dead loop, and server and client interact with each other at all times, even if there is no information to return.
Just keep looking down!
See how the Channel is defined:
Copy Code code as follows:

var message_backlog = 200,
Session_timeout = 60 * 1000;
var channel = new function () {
var messages = [],
callbacks = [];
This.appendmessage = function (nick, type, text) {
var m = {Nick:nick
, Type:type//"MSG", "Join", "part"
, Text:text
, Timestamp: (New Date ()). GetTime ()
};
Switch (type) {
Case "MSG":
Sys.puts ("<" + Nick + ">" + text);
Break
Case "Join":
Sys.puts (Nick + "join");
Break
Case "part":
Sys.puts (Nick + "part");
Break
}
Messages.push (m);
while (Callbacks.length > 0) {
The shift () method is used to remove the first element of an array and return the value of the first element
Callbacks.shift (). Callback ([m]);
}
while (Messages.length > Message_backlog)
Messages.shift ();
};
This.query = function (since, callback) {
var matching = [];
for (var i = 0; i < messages.length; i++) {
var message = Messages[i];
if (Message.timestamp > since)
Matching.push (Message)
}
if (matching.length!= 0) {
callback (matching);
} else {
Callbacks.push ({timestamp:new Date (), callback:callback});
}
};
Clear Old callbacks
They can hang around for at most seconds.
SetInterval (function () {
var now = new Date ();
while (callbacks.length > 0 && now-callbacks[0].timestamp > 30*1000) {
Callbacks.shift (). callback ([]);
}
}, 3000);
};

The channel defines two variables, two methods, and a setinterval function that executes every 3 seconds.
First look at the query method,
The query method receives two parameters:
Since: Record a time
Callback: That is, the anonymous function (JS) in which the call to the Channel.query method is called, the function can be invoked directly when the parameter is passed and received. Do not hurry up the lesson ah ... )
Messages The current chat record queue, the Query method finds the chat records that match the criteria and places them in the matching queue. If Matching.length>0, the callback received function is called, which returns the matching to the client in JSON format. But... Next up is the key!!!
Copy Code code as follows:

if (matching.length!= 0) {
callback (matching);
} else {
Callbacks.push ({timestamp:new Date (), callback:callback});
}

If matching.length<=0, the program puts the callback and the current time in JSON format into a callbacks queue. Right! Does not execute, because there is no eligible chat message, do not need to display in the client, so first put this function to save, do not execute first.
That can not be so kept ah, in case the next second someone send chat message to do?
Then look at the appendmessage (add Chat message) method:
In this method, the first section is very well understood, it is simply to receive incoming parameters, combined into a M set, and then use sys.puts to display in the terminal, and then insert m into the messages chat message queue. And then again, the point:
Copy Code code as follows:

while (Callbacks.length > 0) {
The shift () method is used to remove the first element of an array and return the value of the first element
Callbacks.shift (). Callback ([m]);
}

Now to determine if the callbacks has no storage, if so, execute one, delete one, until the execution is finished. Because a request was made before no chat message could be returned, and the system did not execute the requests, they were placed in the callbacks list.
Now someone has sent a chat message to execute the requests that were not executed again when the method was added.
Popular understanding can be: You send me a request, I do not have new information to answer you, once someone sent a new message, I will answer you immediately.
I don't know if you understand.
This step is over, the last step, is every 3 seconds, empty expired callbacks, is a setinterval function, this is not difficult to understand.
Summarize
Nodejs with its own event-driven language features, to achieve a long link refresh function, let us Open our horizons. The feeling of self benefits greatly. I hereby take the time to write a tutorial to share with you, but also to deepen my own understanding.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.