我正在尝试使用CometD,因为我需要一些经验背景来进行各种推送技术之间的技术比较。
我终于设法在我的Tomcat(v7.0)中运行示例代码。
现在我有一种奇怪的行为,我拼命地试图找出我做错了什么。
以下是我的观察:
连接循环:
我正在与客户端进行无休止的连接循环。
请求:
[{"channel":"/meta/connect","connectionType":"long-polling","id":"19","clientId":"f1le33y91f6pa71f39z52km87yp"}]
响应:
[{"id":"19","successful":true,"advice":{"interval":2000,"reconnect":"retry","multiple-clients":true,"timeout":60000},"channel":"/meta/connect"}]
有趣的是,Listener-callback事件实际上发生了
cometd.addListener('/meta/handshake', _metaHandshake);
客户端连接请求卡住了
这主要发生在重新启动我的Tomcat服务器之后。我可以看到成功订阅2个频道:
请求
[{"version":"1.0","minimumVersion":"0.9","channel":"/meta/handshake","supportedConnectionTypes":["long-polling","callback-polling"],"advice":{"timeout":60000,"interval":0},"id":"1"}]
响应
[{"id":"1","minimumVersion":"1.0","supportedConnectionTypes":["callback-polling","long-polling"],"successful":true,"channel":"/meta/handshake","clientId":"11tnpjnmkqo3tf64zcvufuqbtv","version":"1.0"}]
请求
[{"channel":"/meta/subscribe","subscription":"/hello","id":"2","clientId":"11tnpjnmkqo3tf64zcvufuqbtv"},{"channel":"/service/hello","data":{"name":"World"},"id":"3","clientId":"11tnpjnmkqo3tf64zcvufuqbtv"}]
响应
[{"id":"2","subscription":"/hello","successful":true,"channel":"/meta/subscribe"},{"id":"3","successful":true,"channel":"/service/hello"}]
这一次之后的电话卡住并永远消失。
... abd这是我的代码
客户端
(function($)
{
var cometd = $.cometd;
$(document).ready(function()
{
function _connectionEstablished()
{
$('#body').append('<div>CometD Connection Established</div>');
}
function _connectionBroken()
{
$('#body').append('<div>CometD Connection Broken</div>');
}
function _connectionClosed()
{
$('#body').append('<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)
{
// interesting enough: that event is triggered
alert('event occured');
cometd.batch(function()
{
// that function is called and produces a corresponding request to the server
cometd.subscribe('/hello', function(message)
{
// that one never happens...
$('#body').append('<div>Server Says: ' + message.data.greeting + '</div>');
});
// Publish on a service channel since the message is for the server only
cometd.publish('/service/hello', { name: 'World' });
});
}
}
// Disconnect when the page unloads
$(window).unload(function()
{
cometd.disconnect(true);
});
var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
cometd.configure({
url: cometURL,
logLevel: 'debug'
});
cometd.addListener('/meta/handshake', _metaHandshake);
cometd.addListener('/meta/connect', _metaConnect);
cometd.handshake();
});
})(jQuery);
服务器
请注意,这与分发包中的示例差不多是1:1!
public class HelloService extends AbstractService
{
public HelloService(BayeuxServer bayeux)
{
super(bayeux, "hello");
addService("/service/hello", "processHello");
}
public void processHello(ServerSession remote, Message message)
{
Map<String, Object> input = message.getDataAsMap();
String name = (String)input.get("name");
Map<String, Object> output = new HashMap<String, Object>();
output.put("greeting", "Hello, " + name);
remote.deliver(getServerSession(), "/hello", output, null);
}
}
Web.xml文件
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” 版本= “2.5” &GT;
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>timeout</param-name>
<param-value>60000</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>3</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
也许我只是做了一件完全错误的事情,但我开始有点绝望了。帮助将受到高度赞赏。