我是 cometd 的新手,我计划向服务器发送一条消息并使用 Cometd 在我的浏览器中获取消息如果我发送一条消息,它已成功发送到服务器但无法在浏览器中获取
private void testService() {
String channelName = "/service/out";
log.info("Channel Name = " + channelName);
log.info("bayeuxServer : " + (bayeuxServer == null ? "Is Null" : "Is Not Null"));
System.out.println("CHANNELS : " + bayeuxServer.getChannels().toString());
System.out.println("Subscribers on /service/in = "+bayeuxServer.getChannel("/service/in").getSubscribers().toString());
System.out.println("Subscribers on /service/out = "+bayeuxServer.getChannel("/service/out").getSubscribers().toString());
// convert to cometd format
Map<String, Object> data = new HashMap<String, Object>(4);
data.put("serverMsg", getDetails());
ServerChannel channel = bayeuxServer.getChannel(channelName);
subscribers = channel.getSubscribers().size();
log.info("Subscribers = " + subscribers);
log.info("channel = " + channel);
channel.publish(sender, data, null);
System.out.println("Session subscriptions :" + sender.getServerSession().getSubscriptions());
System.out.println("Listeners on /service/out = "+bayeuxServer.getChannel("/service/out").getListeners().toString());
System.out.println("Subscribers on /service/out = "+bayeuxServer.getChannel("/service/out").getSubscribers().toString());
}
但这一个不起作用
@Subscription("/service/out")
public void echo(Message message)
{
System.out.println("Echo service published " + message);
}
日志:
1
7:48:18,775 INFO ClientHelloService [] Bayeux server =org.cometd.server.BayeuxServerImpl@1436088
17:48:18,775 INFO ClientHelloService [] Message = Hello world
17:48:18,775 INFO ClientHelloService [] remote Client Id = 3renjdwk25ercglzli36tudpl
17:48:18,776 INFO ClientHelloService [] Local session = L:_21w17u5f3mvvluvp71c27yaqvt
17:48:18,776 INFO ClientHelloService [] session = 3renjdwk25ercglzli36tudpl - last connect 1 ms ago
17:48:18,776 INFO ClientHelloService [] Channel Name = /service/out
17:48:18,776 INFO ClientHelloService [] bayeuxServer : Is Not Null
CHANNELS : [/service/out, /meta/subscribe, /service, /service/*, /meta, /meta/handshake, /meta/disconnect, /service/in, /meta/connect, /meta/unsubscribe]
Subscribers on /service/in = []
Subscribers on /service/out = []
17:48:18,777 INFO ClientHelloService [] msg = Hello world
17:48:18,777 INFO ClientHelloService [] Subscribers = 0
17:48:18,777 INFO ClientHelloService [] channel = /service/out
17:48:18,777 DEBUG 21192840 [] < {data={serverMsg=Hello world}, channel=/service/out}
17:48:18,777 INFO ClientHelloService [] publish the channel
Session subscriptions :[]
Listeners on /service/out = []
Subscribers on /service/out = []
应用程序.js
var sendChannel = '/service/in'; // Message from jsp
var receiveChannel = '/service/*'; // Message from server
/*var cometdServerURL = 'http://127.0.0.1:8080/cometd';*/
require(['dojox/cometd', 'dojo/dom', 'dojo/domReady!'], function(cometd, dom)
{ // // configuration object
// cometd.websocketEnabled = true;
// Open connection to CometD server
cometd.configure({
url: location.protocol + '//' + location.host + config.contextPath + '/cometd',
logLevel: 'debug'
});
cometd.addListener('/meta/*', function(message)
{
if (message.successful)
{
dom.byId('status').innerHTML += '<div>CometD handshake successful</div>';
cometd.subscribe(receiveChannel, function(message) {
dom.byId('results').innerHTML +=' Message from server ' + message.data;
dom.byId('results').innerHTML +=' Subscription to ' + receiveChannel;
});
}
else if(_connectionBroken()){
dom.byId('status').innerHTML += '<div>CometD Connection Broken</div>';
}
else
{
dom.byId('status').innerHTML += '<div>CometD handshake failed</div>';
}
});
dom.byId('greeter').onclick = function()
{
var text = dom.byId('msg').value;
cometd.publish(sendChannel, 'Hello world');
dom.byId('msg').value = "" ;
dom.byId('results').innerHTML +='Message send to server' ;
};
cometd.handshake();
});