示例#1
0
 public void PublishMessage()
 {
     var pn = new Pubnub("demo", "demo", string.Empty);
     Assert.IsTrue(pn.Publish("csharp_unit_test", new
     {
         Name = "Example Unit Test Message"
     }));
 }
示例#2
0
        public void Subscribe_HighThroughPut()
        {
            var handle = new ManualResetEvent(false);
            var pn = new Pubnub("demo", "demo", string.Empty);
            var list = new Dictionary<int, object>();
            int count = 10;
            var sync = new object();

            pn.MessageRecieved += (s, e) =>
            {
                Task.Factory.StartNew(() =>
                {
                    if (e.Channel == "csharp_throughput_test")
                    {

                        var o = JObject.Parse(e.Message);
                        System.Diagnostics.Debug.WriteLine(o["ID"].ToString());

                        lock (sync)
                            list.Remove(Convert.ToInt32(o["ID"].ToString()));

                        if (Interlocked.Decrement(ref count) == 0)
                        {
                            System.Threading.Thread.Sleep(1000);
                            handle.Set();
                        }
                    }
                });
            };
            pn.Subscribe("csharp_throughput_test");

            System.Threading.Thread.Sleep(10000);

            Parallel.For(0, 10, (i) =>
            {
                lock (sync)
                    list.Add(i, null);

                // System.Diagnostics.Debug.WriteLine(i);
                var test = (pn.Publish("csharp_throughput_test", new
                {
                    ID = i
                }));

                if (!test)
                {
                    lock (sync)
                        list.Remove(i);
                    System.Diagnostics.Debug.WriteLine("Failed: " + i);
                }

            });

            handle.WaitOne(Convert.ToInt32(new TimeSpan(0, 1, 0).TotalMilliseconds));
            Assert.IsTrue(count == 0);
        }
示例#3
0
        public void PublishMessage_Parallel()
        {
            var pn = new Pubnub("demo", "demo", string.Empty);

            Parallel.For(0, 5, (i) =>
            {
                pn.Publish("csharp_unit_test", new
                {

                });
            });
        }
示例#4
0
        static void Main(string[] args)
        {
            Console.WriteLine("What is your chat name?");
            var chatName = Console.ReadLine();

            // Setup a Chatroom
            var pubnub = new Pubnub("demo", "demo", string.Empty);

            // Setup an event handler
            pubnub.MessageRecieved += (s, e) =>
            {
                Task.Factory.StartNew(() =>
                {
                    if (e.Channel == Program.ChatChannel)
                    {
                        var message = JsonConvert.DeserializeObject<ChatMessage>(e.Message);
                        if (message.FromUuid != Uuid)
                        {
                            switch (message.Type)
                            {
                                case "ping":
                                    UpdateTime(message.FromUuid);
                                    break;

                                case "leaving":
                                    if (people.ContainsKey(message.FromUuid))
                                        lock (sync)
                                            people.Remove(message.FromUuid);

                                    break;

                                case "joined":
                                    if (!people.ContainsKey(message.FromUuid))
                                    {
                                        lock (sync)
                                            people.Add(message.FromUuid, message.From);
                                        Console.WriteLine(message.From + " has joined the chat");
                                    }
                                    UpdateTime(message.FromUuid);
                                    break;

                                case "message":
                                    UpdateTime(message.FromUuid);
                                    Console.WriteLine(string.Format("{0} said:\n{1}",
                                                                    message.From,
                                                                    message.Message));

                                    break;
                            }
                        }
                    }
                });
            };

            pubnub.Publish(Program.ChatChannel, new ChatMessage
            {
                FromUuid = Uuid,
                From = chatName,
            });

            if (!pubnub.Subscribe(Program.ChatChannel))
                throw new Exception("Could not subscribe to channel: " + Program.ChatChannel);

            pubnub.Publish(Program.ChatChannel, new ChatMessage
            {
                Type = "joined",
                From = chatName,
                FromUuid = Uuid
            });

            // to keep all the other clients up to date, let's
            // remind them we're here every 30 seconds, otherwise
            // they'll forget we exist within 60 seconds
            Task.Factory.StartNew(() => {

                using (handle = new AutoResetEvent(false))
                {
                    while (Running)
                    {
                        // let everyone know that we're still here
                        pubnub.Publish(Program.ChatChannel, new ChatMessage
                        {
                            Type = "ping",
                            From = chatName
                        });

                        // check our list of people, and if there is anyone
                        // who hasn't checked in over 60 seconds.
                        foreach (var kvp in peopleHistory)
                        {
                            if ((DateTime.Now - kvp.Value).TotalSeconds >= 60)
                                lock (sync)
                                    if (people.ContainsKey(kvp.Key))
                                        people.Remove(kvp.Key);
                        }

                        // now remove any people from the history list, who
                        // are not in the regular list
                        lock (sync)
                        {
                            var list = peopleHistory.Keys.ToList();
                            for (var i = 0; i < peopleHistory.Keys.Count; i++)
                                if (!people.ContainsKey(list[i]))
                                    peopleHistory.Remove(list[i]);
                        }
                        handle.WaitOne(new TimeSpan(0, 0, 30));
                    }
                }

            });

            start:
            var read = Console.ReadLine();
            switch (read.ToLower())
            {
                case "!pm":
                    // send a 'private' message to someone (not really private
                    // as the message is delivered to all clients, but only
                    // displayed on one)

                    goto start;

                case "!q":
                case "!quit":
                    break;

                default:
                    // by default let's send a message to the chat channel
                    // Send a disconnection message
                    pubnub.Publish(Program.ChatChannel, new ChatMessage
                    {
                        Type = "message",
                        From = chatName,
                        FromUuid = Uuid,
                        Message = read
                    });

                    goto start;
            }

            // Stop the announcement loop
            Running = false;
            if (handle != null)
                handle.Set();

            // Send a disconnection message
            pubnub.Publish(Program.ChatChannel, new ChatMessage
            {
                Type = "leaving",
                From = chatName,
                FromUuid = Uuid
            });
        }