示例#1
0
        // publish a message to a channel
        public void publish_channel(publish p)
        {
            if (protocol.pubsub != null)
            {
                // attempt to pull the channel from pubsub
                PubSub_Channel c = protocol.pubsub.GetChannel(p.channel_name_or_uri);

                // make sure channel exists
                if (c != null)
                {
                    // create the pubsub event
                    PubSub_Event e = new PubSub_Event(p.message);

                    // add the event to the channel
                    c.add_event(e);

                    // generate event object
                    _event ev = new _event(p.channel_name_or_uri, e);

                    // setup subscribers list
                    List<WS3V_Client> subscribers;

                    if (p.echo)
                        // blast the event to all subscribed
                        subscribers = protocol.WS3V_Clients.Select(g => g.Value).Where(s => s.subscriptions != null && s.subscriptions.Any(y => y.channel_name_or_uri == p.channel_name_or_uri)).ToList();

                    else
                        // blast the event to all subscribed except current client
                        subscribers = protocol.WS3V_Clients.Select(g => g.Value).Where(s => s.protocol.clientID != protocol.clientID && s.subscriptions != null && s.subscriptions.Any(y => y.channel_name_or_uri == p.channel_name_or_uri)).ToList();

                    // serialize event
                    string msg = ev.ToString();

                    // loop over clients and send message blast
                    for (int i = 0; i < subscribers.Count; i++)
                        subscribers[i].SocketSend(msg);
                }

            }
        }
示例#2
0
        public void _event()
        {
            // documentation example 1
            _event e = new _event("/rss/news/latest", new PubSub_Event("Singer Phil Collins Announces Retirement", 41267360));
            string expected = "[16,\"\\/rss\\/news\\/latest\",\"Singer Phil Collins Announces Retirement\",41267360]";
            string result = e.ToString();
            Assert.AreEqual(expected, result);

            // documentation example 2
            e = new _event("/rss/news/latest", new PubSub_Event("{\"title\":\"Singer Phil Collins Announces Retirement\",\"description\":\"Phil Collins has decided to end his career after ...\",\"url\":\"http:\\/\\/examplenews.co.uk\\/phil-collins-announces-retirement\"}", 41267360));
            expected = "[16,\"\\/rss\\/news\\/latest\",{\"title\":\"Singer Phil Collins Announces Retirement\",\"description\":\"Phil Collins has decided to end his career after ...\",\"url\":\"http:\\/\\/examplenews.co.uk\\/phil-collins-announces-retirement\"},41267360]";
            result = e.ToString();
            Assert.AreEqual(expected, result);
        }