// subscribes a client to a channel(s) public void subscribe_channel(string[] message) { // extract subscription(s) requested subscribe s = new subscribe(message); // pubsub enabled if (protocol.pubsub != null) { // itterate over the channels requested by the client for (int i = 0; i < s.channel_name_or_uri.Length; i++) { // call the dynamic subscription builder protocol.Subscribe(s.channel_name_or_uri[i]); // attempt to pull the channel from pubsub PubSub_Channel c = protocol.pubsub.GetChannel(s.channel_name_or_uri[i]); // we need to see if the channel exists if (c != null) { // subscribe the client to this channel if (subscriptions == null) subscriptions = new List<PubSub_Channel>(); subscriptions.Add(c); // subscribe the client and pass true to allow publishing protocol.SocketSend(new acknowledge(c, true).ToString()); } else // channel does not exist, send deny not found protocol.SocketSend(new deny(s.channel_name_or_uri[i], new PubSub_Exception(404, "Not Found", "http://example.com/api/error#404")).ToString()); } } else { // pubsub not enabled or not wired up correctly, send deny messages for (int i = 0; i < s.channel_name_or_uri.Length; i++) // we need to send a deny down each channel, we gurantee a response protocol.SocketSend(new deny(s.channel_name_or_uri[i], new PubSub_Exception(403, "Forbidden", "http://example.com/api/error#403")).ToString()); } }
public void subscribe() { // example 1 string input = "[10,[\"\\/rss\\/news\\/latest\"]]"; string[] message = JSONDecoders.DecodeJSONArray(input); Assert.AreEqual(message[0], "10"); subscribe s = new subscribe(message); Assert.AreEqual(s.channel_name_or_uri[0], "/rss/news/latest"); // example 2 input = "[10,[\"\\/rss\\/news\\/latest\",\"\\/user\\/28\\/comments\",\"\\/user\\/29\\/comments\"],[{\"category\": \"technology\"},{\"public\": true,\"private\": false},{\"public\": false,\"private\": true}]]"; message = JSONDecoders.DecodeJSONArray(input); Assert.AreEqual(message[0], "10"); s = new subscribe(message); Assert.AreEqual(s.channel_name_or_uri[0], "/rss/news/latest"); Assert.AreEqual(s.channel_name_or_uri[1], "/user/28/comments"); Assert.AreEqual(s.channel_name_or_uri[2], "/user/29/comments"); }