cometd:发布到频道的消息未显示在订阅的侦听器中

时间:2018-02-19 07:12:02

标签: groovy cometd

我正在尝试将测试数据发布到cometd-demo服务器频道members/hello/。握手完成,可以在callback上获得订阅的消息,并可以在publish()回调上获得发布的消息。但我无法在subscribe()听众那里收到发布的消息。

<小时/> Groovy脚本:

import org.cometd.bayeux.Message;
import org.cometd.bayeux.Message.Mutable
import org.cometd.bayeux.client.ClientSessionChannel;
import org.cometd.bayeux.client.ClientSessionChannel.MessageListener;
import org.cometd.client.BayeuxClient
import org.cometd.client.transport.ClientTransport
import org.cometd.client.transport.LongPollingTransport
import org.eclipse.jetty.client.HttpClient as MyHttpClient

ClientSessionChannel.MessageListener mylistener = new Mylistener();

def myurl = "http://localhost:8080/cometd/"

MyHttpClient httpClient = new MyHttpClient();

httpClient.start()

Map<String, Object> options = new HashMap<String, Object>();

ClientTransport transport = new LongPollingTransport(options, httpClient);

BayeuxClient client = new BayeuxClient(myurl, transport)

client.handshake(30000)

def channel = client.getChannel("/members/hello/")

channel.subscribe(mylistener,mylistener)  

while (true)

{

    sleep(5000)

    channel.publish( 'hai' )

}

class Mylistener implements ClientSessionChannel.MessageListener {

        public void onMessage(ClientSessionChannel channel, Message message) {

            println message

        }

    }

在运行此脚本时,我无法在侦听器上获取已发布的数据,即使JVM未被while循环杀死。我错过了什么?

1 个答案:

答案 0 :(得分:1)

您在:

中指定了错误的频道路径
def channel = client.getChannel("/members/hello/")

频道路径不能以/结尾 - 它应为/members/hello

同时仔细检查您是否使用了正确的网址。我使用了非常简单的使用/dojo-jetty9-primer/上下文路径的CometD服务器应用程序(https://github.com/wololock/dojo-jetty9-primer),所以在我的案例中,CometD服务器的URL是:

def url = "http://localhost:8080/dojo-jetty9-primer/cometd/"

您还可以将脚本简化为:

import org.cometd.bayeux.Message
import org.cometd.bayeux.client.ClientSessionChannel
import org.cometd.client.BayeuxClient
import org.cometd.client.transport.LongPollingTransport
import org.eclipse.jetty.client.HttpClient

final String url = "http://localhost:8080/dojo-jetty9-primer/cometd/"

final HttpClient httpClient = new HttpClient()
httpClient.start()

final BayeuxClient client = new BayeuxClient(url, new LongPollingTransport([:], httpClient))
client.handshake()
client.waitFor(1000, BayeuxClient.State.CONNECTED)

final ClientSessionChannel channel = client.getChannel("/members/hello")
channel.subscribe(new MyListener())

while (true) {
    sleep(1000)
    channel.publish("test")
}

class MyListener implements ClientSessionChannel.MessageListener {
    @Override
    void onMessage(ClientSessionChannel channel, Message message) {
        println "[${new Date()}] Received message from channel (${channel.id}): ${message}"
    }
}

特别是在您的脚本中可以简化部分client.handshake(30000) - 您不必在此等待30秒。

当你运行它时,你会看到每1秒在控制台中显示一条新消息:

[Mon Feb 19 10:15:02 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
[Mon Feb 19 10:15:03 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
[Mon Feb 19 10:15:04 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
[Mon Feb 19 10:15:05 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
[Mon Feb 19 10:15:06 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]

希望它有所帮助。