Comet Jetty / Tomcat,Firefox和Chrome浏览器有一些浏览器问题 [英] Comet Jetty/Tomcat, having some browser issues with Firefox and Chrome

查看:213
本文介绍了Comet Jetty / Tomcat,Firefox和Chrome浏览器有一些浏览器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索使用Comet作为我正在开发的项目。

我尝试先使用Tomcat6和CometProcessor API创建测试应用程序,然后使用Jetty7继续。

该应用程序是两种工作,但我有一些与消息的实际显示的问题。

我使用创建一个XMLHttpRequest连接并保持它始终打开的技术,使服务器可以不断推数据发送到所有连接的客户端,只要有可用。



我的客户端代码与此类似:

 函数fn(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){

if(xhr.readyState == 3){
document.getElementById('dv')。innerHTML =(xhr.responseText) ;

if(xhr.readyState == 4){
alert('done');


xhr.open(GET,First,true);
xhr.send(null);
}

我在某处使用了 readyState 3 线上。



我目前面临两个问题:


  1. Firefox 这个代码完美地工作。但是如果我打开一个新的标签,甚至一个新的浏览器窗口,它不会建立新的连接到服务器,新的标签页或窗口没有任何显示,只有第一个标签页/窗口获取显示。我使用 wireshark来检查这个,即使打开第二个标签,它也只显示1个连接。我无法理解为什么会发生这种情况。我已经阅读了关于2个连接的限制,但是这里只有一个连接。

  2. 其次,在 Chrome 中,上面的代码确实不工作,只有当服务器关闭连接时,我才会得到输出

,所以回调函数不会被调用3 的readystate。 >

我也想问一下用Comet做Java的最好方法/框架。我目前在客户端使用jQuery。

任何建议将不胜感激!谢谢

解决方案

Chrome的这种有线行为真的很烦人。我试图找出GMail(谷歌自己的应用程序)如何在Chrome中实现Comet,但是没有合适的Http Sniffer可以永久捕获Chrome的HTTP通信。



解决方案1:我原来的想法:



我们可以在Comet Http响应中使用Content-Type:multipart / x-mixed-replace我测试了它。如果响应是多段的,当( xhr.readyState == 3 )为true时, xhr.responseText 不为空。

唯一的问题是, xhr.responseText 是整个响应,而不是Firefox的替换响应。例如,服务器发送A,然后B替换A,然后C替换B。在Firefox中,当 xhr.readyState == 4 时,您将得到A,B,C。在Chrome浏览器中,当 xhr.readyState == 3时,您将获得A,AB和ABC。 b
客户端JavaScript应该解析 xhr.responseText 来提取推送的数据。

解决方案2:
这是Safari推荐的 http://lists.macosforge.org/pipermail/webkit- dev / 2007-June / 002041.html


Webit引擎在显示足够的字节之前不会呈现推送的数据。据称需要初始的256字节的填充。我尝试在Chrome(4.1.249.1036(41514))。它看起来大约需要1千字节的第一个推送的有效载荷触发器(readyState == 3)。

确保XHR不直接发送onload事件处理程序。否则,页面的标题或网址栏中会显示加载指示符。


I am exploring the use of Comet for a project I am working on.
I tried creating a test application first using Tomcat6 and CometProcessor API and then with Jetty7 Continuations.
The application is kind of working on both but I am having some issues with the actual display of messages.
I used the technique of creating an XMLHttpRequest Connection and keeping it open all the time so the server can continuously push data to all the clients connected whenever it is available.

My client side code is something similar to this:

function fn(){
var xhr = new XMLHttpRequest();
 xhr.onreadystatechange = function(){

  if (xhr.readyState==3){
document.getElementById('dv').innerHTML =(xhr.responseText);
}
if (xhr.readyState==4){
alert ('done');
}
}
xhr.open("GET", "First", true);
xhr.send(null);
}

I found this thing of using readyState 3 somewhere online.

I am facing 2 problems currently:

  1. In Firefox this code works perfectly. But if I open a new tab or even a new browser window, it does not make a new connection to the server and nothing shows up on the new tab or window, only the first tab/window gets the display. I used wireshark to check this and its shows only 1 connection even after the 2nd tab is opened. I am unable to understand why this would happen. I have read about the 2 connection limit, but here there is only one connection.

  2. Secondly in Chrome, the above code does not work, and the callback is not invoked for readystate of 3, only when the connection is closed by the server i get the output.

I would also like to ask which is the best way/framework for doing Comet with Java. I am currently using jQuery on the client side.
Any suggestions would be greatly appreciated!! Thanks

解决方案

This wired behavior of Chrome is really annoying. I tried to find out how GMail(Google's own application) implements Comet in Chrome, but there is no appropriate Http Sniffer to catch forever HTTP traffic of Chrome.

Solution 1: My original thought:

We can have "Content-Type: multipart/x-mixed-replace" header in Comet Http response. I tested it. If the response is multiparted, xhr.responseText is not empty when (xhr.readyState == 3) is true.

The only problem is that xhr.responseText is the whole response instead of "replaced" response as Firefox does. For example, the server send "A", then "B" to replace "A", then "C" to replace "B". In Firefox, you will get "A", "B", "C" when xhr.readyState==4. In Chrome, you will get "A", "AB" and "ABC" when xhr.readyState == 3.

So, you client javascript should to parse xhr.responseText to extract pushed data.

Solution 2: This is recommend by Safari http://lists.macosforge.org/pipermail/webkit-dev/2007-June/002041.html.

Webit engine doesn't render pushed data until there are enough bytes to show. It is claimed to need initial 256 bytes padding. I tried in Chrome (4.1.249.1036 (41514)). It looks that about 1 kilo bytes is needed to have the first pushed payload trigger (readyState == 3).

Make sure the XHR is not sent directly sent in onload event handler. Otherwise, there is loading indicator in title or URL bar for the page.

这篇关于Comet Jetty / Tomcat,Firefox和Chrome浏览器有一些浏览器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆