示例#1
0
        /// <summary>
        /// 1개이상의 Topic 을 Subscriber 할 경우 사용
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="type"></param>
        /// <param name="msgHandler"></param>
        /// <returns></returns>
        public async Task AddSubscriber(string topic, string type, MessageReceivedHandler msgHandler)
        {
            try
            {
                if (!Data.Instance.isConnected)
                {
                    return;
                }
                bool bcheck = false;
                foreach (Subscriber subitem in Data.Instance.subs)
                {
                    //같은 Topic 명이 있을 경우 subscriber에 추가 하지 않음.
                    if (subitem.Topic == topic)
                    {
                        return;
                    }
                }

                rosSubscriber sub = new rosSubscriber(topic, type, Data.Instance.md);
                Data.Instance.subs.Add(sub);

                sub.SetMessageHandler(msgHandler);
                await sub.SubscribeAsync();
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("AddSubscriber err :={0}", ex.Message.ToString());
            }
        }
示例#2
0
        public async Task DeleteSubscriber(string topic)
        {
            try
            {
                int i         = 0;
                int removeIdx = 0;
                for (i = 0; i < Data.Instance.subs.Count; i++)
                {
                    if (null != Data.Instance.subs[i])
                    {
                        rosSubscriber sub = (rosSubscriber)Data.Instance.subs[i];

                        if (Data.Instance.subs[i].Topic == topic)
                        {
                            sub.FreeMessageHandler();
                            await sub.UnsubscribeAsync();

                            //await Data.Instance.subs[i].UnsubscribeAsync();
                            removeIdx = i;
                        }
                    }
                }
                Data.Instance.subs.RemoveAt(removeIdx);
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("DeleteSubscriber Topic err :={0}", ex.Message.ToString());
            }

            /*  foreach (rosSubscriber sub in Data.Instance.subs)
             * {
             *    if (null != sub)
             *    {
             *        if (sub.Topic == topic)
             *        {
             *            sub.FreeMessageHandler();
             *            await sub.UnsubscribeAsync();
             *        }
             *    }
             * }*/
        }