在JAVA中长时间轮询Jquery? [英] Long polling Jquery in JAVA?
问题描述
这是我的Java Chat application
的问题.
启动应用程序后,我将在外部Jquery中调用pingAction()
.
Jquery pingAction
将是
function pingAction(){
$.ajax(
{
type: "post",
url: "PingAction",
async: false,
data : "userId="+encodeURIComponent(userId)+"&secureKey="+encodeURIComponent(secureKey)+"&sid="+Math.random() ,
cache:false,
complete: pingAction,
timeout: 5000 ,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
scriptCharset: "utf-8" ,
dataType: "html",
error: function (xhr, ajaxOptions, thrownError) {
alert("xhr.status : "+xhr.status);
if(xhr.status == 12029 || xhr.status == 0){
//alert("XMLHttp status : "+xhr.status);
$("#serverMsg").css("backgroundColor" , "yellow");
$("#serverMsg").text("Your Network connection is failed !");
$("#serverMsg").show();
}
//setTimeout('pingAction()', 5000);
xhr.abort();
},
success: function( responseData , status){
if($("#serverMsg").text() == "" || $("#serverMsg").text() == "Your Network connection is failed !"){
disableServerMessage();
}
if(responseData != "null" && responseData.length != 0 && responseData != null){
var stringToArray = new Array;
stringToArray = responseData.split("<//br//>");
var len = stringToArray.length;
for(var i=0;i<len-1;i++){
getText(stringToArray[i]);
}
}
//setTimeout('pingAction()', 5000);
}
}
);
}
我的PingAction Servlet
将是,
public class PingAction extends HttpServlet {
private static final long serialVersionUID = 1L;
private String secureKey;
private String userId;
private int fromPosition ;
private FlexChatProtocol protocol = null;
private Ping ping = null;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
secureKey = request.getParameter("secureKey");
userId = request.getParameter("userId");
CustomerInfo customer = ApplicationInfo.customerDetails.get(userId);
if(customer != null){
fromPosition = customer.getFromPosition();
}
if(ApplicationInfo.flexProtocol != null ){
protocol = ApplicationInfo.flexProtocol;
ping = new Ping();
ping.sendPing(secureKey, userId, fromPosition+1, protocol, serverMessage);
if(customer != null){
message = customer.getFullMessage();
}
out.println(message);
}
}
}
在使用Long Poling
之前,我将使用setTimeInterval()
为每个5 seconds
调用pingAction() in javaScript
,以刷新连接并获取服务器消息.
现在我需要在聊天应用程序中使用LONG POLLING concept
.因此,我修改了上面看到的Jquery pinAction()
.
如何使用JQUERY
实现LONG POLLING
?
希望我们的堆栈成员会帮助我!
private ChatContext context = ChatContext.getInstance();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Long lastmessage = // just put this somewhere
List<String> messages = context.getMessagesIHaventGotYet(lastmessage); // blocking call
Object formatedMessages = formatmessages(messages);
out.write(formatedMessages);
}
context.getMessagesIHaventGotYet();应该是一个阻止操作,因此它将一直等待,直到有新消息到达,然后开始执行. 或类似的东西.
基本长轮询意味着服务器挂起",直到从某处检索到所需信息,然后将其写入其输出缓冲区并关闭连接,客户端再次实例化连接,以开始另一个长轮询. /p>
This is the question for my Java Chat application
.
I will call the pingAction()
in my external Jquery when my application get initiated.
The Jquery pingAction
will be ,
function pingAction(){
$.ajax(
{
type: "post",
url: "PingAction",
async: false,
data : "userId="+encodeURIComponent(userId)+"&secureKey="+encodeURIComponent(secureKey)+"&sid="+Math.random() ,
cache:false,
complete: pingAction,
timeout: 5000 ,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
scriptCharset: "utf-8" ,
dataType: "html",
error: function (xhr, ajaxOptions, thrownError) {
alert("xhr.status : "+xhr.status);
if(xhr.status == 12029 || xhr.status == 0){
//alert("XMLHttp status : "+xhr.status);
$("#serverMsg").css("backgroundColor" , "yellow");
$("#serverMsg").text("Your Network connection is failed !");
$("#serverMsg").show();
}
//setTimeout('pingAction()', 5000);
xhr.abort();
},
success: function( responseData , status){
if($("#serverMsg").text() == "" || $("#serverMsg").text() == "Your Network connection is failed !"){
disableServerMessage();
}
if(responseData != "null" && responseData.length != 0 && responseData != null){
var stringToArray = new Array;
stringToArray = responseData.split("<//br//>");
var len = stringToArray.length;
for(var i=0;i<len-1;i++){
getText(stringToArray[i]);
}
}
//setTimeout('pingAction()', 5000);
}
}
);
}
My PingAction Servlet
will be ,
public class PingAction extends HttpServlet {
private static final long serialVersionUID = 1L;
private String secureKey;
private String userId;
private int fromPosition ;
private FlexChatProtocol protocol = null;
private Ping ping = null;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
secureKey = request.getParameter("secureKey");
userId = request.getParameter("userId");
CustomerInfo customer = ApplicationInfo.customerDetails.get(userId);
if(customer != null){
fromPosition = customer.getFromPosition();
}
if(ApplicationInfo.flexProtocol != null ){
protocol = ApplicationInfo.flexProtocol;
ping = new Ping();
ping.sendPing(secureKey, userId, fromPosition+1, protocol, serverMessage);
if(customer != null){
message = customer.getFullMessage();
}
out.println(message);
}
}
}
Before using the Long Poling
, I will call the pingAction() in javaScript
for every 5 seconds
using the setTimeInterval()
to refresh the connection and getting the server message.
Now I need to use the LONG POLLING concept
in the Chat application.So I modified my Jquery pinAction()
what you have seeing above.
How can I achieve LONG POLLING
using the JQUERY
?
Hope our stack members will help me !
private ChatContext context = ChatContext.getInstance();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Long lastmessage = // just put this somewhere
List<String> messages = context.getMessagesIHaventGotYet(lastmessage); // blocking call
Object formatedMessages = formatmessages(messages);
out.write(formatedMessages);
}
context.getMessagesIHaventGotYet(); should be a blocking operation so it keeps on waiting untill new messages arrive, then it kicks into action. or something along those lines.
Basicly long polling means the server "hangs" untill the information it needs has been retrieved from somewhere, then it writes it in its output buffer and closes the connection, the client instantiates the conneciton ASAP again to start another long poll.
这篇关于在JAVA中长时间轮询Jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!