Cometd Java Client won’t publish to Cometd server

I followed the examples that I could find for making a Cometd java client application. I am trying to make it so when the user presses a button on the screen, cometd publishes a message to the server. Right now the publish gets called but the server never receives message to its listener. I have this server listener working with javascript but not java code.

Client side setup is as follows:

        // 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());

My handshake and connection listeners:

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();
            }
        }
    }

Button press that tries to publish:

_btnPlay.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(_connection_established == true)
                {
                    Map<String, Object> data = new HashMap<String, Object>();
                    _clientSessionChannel.publish(data);
                }
            }
         });

Just in case here is my server Listener too:

@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);  
                }
            }
        }
    }

So I can establish a connection and my publish gets called, but the server never picks it up. Also I noticed if I do the same exact publish but in my handshake or connect callback, the server will pick it up. Seems like I just can’t do a publish outside of the callbacks.

Thanks in Advance for the Help.

Answer

It turned out that I was running the network, cometD code, on Androids Main UI Thread and that is not allowed. Once I moved all the cometd code over to an AsyncTask it all started working all the time.

Leave a Reply

Your email address will not be published. Required fields are marked *