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

cometd: published message to a channel does not show up in subscribed listener

我正在尝试将测试数据发布到 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 循环杀死。我错过了什么?

您指定的频道路径不正确:

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

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

还要仔细检查您使用的是否正确 URL。我使用了非常简单的 CometD 服务器应用程序 (https://github.com/wololock/dojo-jetty9-primer),它使用 /dojo-jetty9-primer/ 上下文路径,所以在我的情况下 URL 到 CometD 服务器是:

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]

希望对您有所帮助。