Cannot receive CometD messages on .NET

被刻印的时光 ゝ 提交于 2019-12-13 17:41:22

问题


I am developing WinForm desktop application. I am successfully handshaking with CometD server and then sending change requests to the server. I am sure that requests are successfully received and request command is done by the server and finally expecting that there is a State Changed message by the CometD server. However, there is no message received to me onMessage() method. Does anyone know why? Any suggestions?

public void getCometDAuthenticationRequest(HttpWebRequest request)
    {
        request = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["MyServer"]);
        request.ContentType = "application/json; charset=utf-8";
        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
        request.PreAuthenticate = true;
        request.Method = "POST";
    }

    private void connectToCometD()
    {
        try
        {
            IClientSessionChannel channel;
            BayeuxClient client;

            Action<HttpWebRequest> customizeRequest = new Action<HttpWebRequest>(getCometDAuthenticationRequest);
            LongPollingTransport transport = new LongPollingTransport(null, customizeRequest);

            // Handshake
            string url = ConfigurationManager.AppSettings["CometDServer"];
            client = new BayeuxClient(url, new List<ClientTransport>() { transport });

            client.handshake();
            client.waitFor(1000, new List<BayeuxClient.State>() { BayeuxClient.State.CONNECTED });

            // Subscription to channels
            channel = client.getChannel("/fooChannel");
            channel.subscribe(new Listener());               

        }
        catch (Exception e)
        {
            MessageBox.Show("EXCEPTION ON connectToCometD(): " + e.Message);
            return;
        }            
    }

class Listener : IMessageListener
{
    public void onMessage(IClientSessionChannel channel, IMessage message)
    {                         
            MessageBox.Show("New Message received.. Message:" + message.ToString());             
    }       
}

来源:https://stackoverflow.com/questions/53441782/cannot-receive-cometd-messages-on-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!