我按照我可以找到的用于制作 Cometd java 客户端应用程序的示例进行操作。我试图做到这一点,所以当用户按下屏幕上的按钮时,cometd 会向服务器发布一条消息。现在发布被调用,但服务器永远不会收到消息给它的监听器。我有这个服务器监听器使用 javascript 但不是 java 代码。
客户端设置如下:
// Prepare the transport
Map<String, Object> options = new HashMap<String, Object>();
ClientTransport transport = LongPollingTransport.create(options, httpClient);
_client = new BayeuxClient("http://10.100.97.168:8888/slideshow/slideshow/", transport);
_client.getChannel(Channel.META_HANDSHAKE).addListener(new InitializerListener());
_client.getChannel(Channel.META_CONNECT).addListener(new ConnectionListener());
我的握手和连接监听器:
private class InitializerListener implements ClientSessionChannel.MessageListener
{
public void onMessage(ClientSessionChannel channel, Message message)
{
if (message.isSuccessful())
{
_handshaked = true;
}
else
{
_handshaked = false;
}
}
}
private class ConnectionListener implements ClientSessionChannel.MessageListener
{
private boolean wasConnected;
private boolean connected;
public void onMessage(ClientSessionChannel channel, Message message)
{
if (_client.isDisconnected())
{
connected = false;
connectionClosed();
return;
}
wasConnected = connected;
connected = message.isSuccessful();
if (!wasConnected && connected)
{
connectionEstablished();
}
else if (wasConnected && !connected)
{
connectionBroken();
}
}
}
尝试发布的按钮按下:
_btnPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(_connection_established == true)
{
Map<String, Object> data = new HashMap<String, Object>();
_clientSessionChannel.publish(data);
}
}
});
以防万一这也是我的服务器监听器:
@Listener("/service/slide/play")
public void processServiceSlidePlay(ServerSession client, ServerMessage message)
{
synchronized(imagelock)
{
if(slideShowRunning == false && imageIDList != null && imageIDList.size() > 0)
{
slideShowRunning = true;
if(imageIDList != null && imageIDList.size() > 0)
{
deliverChangeMessage(client, message);
}
}
}
}
所以我可以建立一个连接并调用我的发布,但服务器永远不会接听它。我还注意到,如果我执行相同的确切发布,但在握手或连接回调中,服务器会接收它。似乎我不能在回调之外进行发布。
在此先感谢您的帮助。