我有一个基于Cometd的PUSH机制已经在Spring-MVC应用程序上工作。目前,只要打开新选项卡并且用户尝试与新选项卡聊天,第一个选项卡上的连接就会丢失,因为每个浏览器的多个会话设置为false。为此,我尝试更改设置,并将其添加到web.xml中,但它无法正常工作。有什么想法吗?
web.xml:
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometDServlet</servlet-class>
<init-param>
<param-name>maxSessionsPerBrowser</param-name>
<param-value>-1</param-value>
</init-param>
<init-param>
<param-name>allowMultiSessionsNoBrowser</param-name>
<param-value>true</param-value>
</init-param>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/cometd/*</url-pattern>
</filter-mapping>
这没有帮助,第一个标签仍然无法连接。我究竟做错了什么?谢谢。
修改的
加载新窗口或标签时调用的代码:
(function($)
{
$(document).ready(function()
{
var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
var connectionIntervall = null;
var onlineElement = document.getElementById("browser-online");
cometd.configure({
url: cometURL,
logLevel: 'debug'
});
cometd.addListener('/meta/handshake', _metaHandshake);
cometd.addListener('/meta/connect', _metaConnect);
cometd.handshake();
function _connectionEstablished()
{
clearInterval(connectionIntervall);
onlineElement.className="";
}
function _connectionBroken()
{
connectionIntervall = setInterval(function(){
onlineElement.className="offline";
}, 1333);
}
function _connectionClosed()
{
//$('.content-container').prepend('<div>CometD Connection Closed</div>');
}
// Function that manages the connection status with the Bayeux server
var _connected = false;
function _metaConnect(message)
{
if (cometd.isDisconnected())
{
_connected = false;
_connectionClosed();
return;
}
var wasConnected = _connected;
_connected = message.successful === true;
if (!wasConnected && _connected)
{
_connectionEstablished();
}
else if (wasConnected && !_connected)
{
_connectionBroken();
}
}
// Function invoked when first contacting the server and
// when the server has lost the state of this client
function _metaHandshake(handshake)
{
if (handshake.successful === true)
{
cometd.batch(function()
{
cometDModel.subscribeToCometD();
chatView.createSoul("");
});
}
}
// Disconnect when the page unloads
$(window).unload(function()
{
cometd.disconnect(true);
});
});
})(jQuery);
}