コード例 #1
0
ファイル: TestMisc.cs プロジェクト: 2spark/SparklrSharp
        public async Task TestAwake()
        {
            Connection conn = new Connection();

            bool result = await conn.GetAwakeAsync();

            Assert.IsTrue(result);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: yonglehou/SparklrSharp
        private static async void mainLoop()
        {
            //Create a new connection
            Connection conn = new Connection();
            bool authenticated = false;

            //Check if sparklr is online
            if (!await conn.GetAwakeAsync())
            {
                Console.WriteLine("Sparklr is offline :(");
            }
            else
            {
                Console.WriteLine("Sparklr is online");

                //Now we need to authenticate
                while (!authenticated)
                {
                    suppressOutput = true;
                    Console.WriteLine("Username:"******"Password:"******"Signed in");

                while (conn.CurrentUser == null)
                    Thread.Sleep(100);

                //Get the number of notifications
                Notification[] notifications = await conn.GetNotificationsAsync();

                Console.WriteLine("You have {0} notifications:", notifications.Length);

                foreach (Notification n in notifications)
                    Console.WriteLine("\t{0}", n.NotificationText);

                /*
                 * Now we can get a list of all messages for example.
                 * The Inbox is associated with the connection to allow
                 * applications where multiple connections exist in parallel.
                 * First you need to refresh the inbox manually.
                 */

                Console.WriteLine("Refreshing inbox");
                await conn.RefreshInboxAsync();

                //After the refresh we can dump the messages.
                Console.WriteLine("You have {0} conversations.", conn.Inbox.Count);

                int index = 0;
                foreach (Message m in conn.Inbox)
                {
                    Console.WriteLine("{3}: Converstation with {0}(@{1}):\t{2}", m.Author.Name, m.Author.Handle, m.Content, index);
                    index++;
                }

                //We now let the user select a conversation he wants to read on the console.
                Console.WriteLine("Enter the conversation you want to read.");

                suppressOutput = true;
                int selected = Convert.ToInt32(Console.ReadLine());
                suppressOutput = false;

                if (selected < index && selected >= 0)
                {
                    Console.WriteLine("Retreiving conversation with {0}.", conn.Inbox[selected].Author.Name);

                    //We retreive the appropriate connection.
                    Conversation conversation = conn.GetConversationWith(conn.Inbox[selected].Author);

                    Console.WriteLine("Loading messages...");

                    //Now we can load more messages until everything is loaded. Messages will be loaded in chunks of 30 messages per request.
                    //You most likely don't even need all messages, so you can load them on demand

                    //There is also an extension method that returns an IEnumerable<Message>, it is however synchronous.
                    //You can retreive the IEnumerable by calling conn.ConversationWith(USER) and using it driectly
                    //Example: foreach(Message m in conn.ConversationWith(USER))

                    while (conversation.NeedsRefresh)
                    {
                        Console.WriteLine("Loading more messages, we currently loaded {0} messages in total", conversation.Messages.Count);
                        await conversation.LoadMore();
                    }

                    suppressOutput = true;
                    //Now we can iterate over all retreived messages and print them to the console.
                    foreach (Message m in conversation.Messages)
                    {
                        Console.WriteLine("{0}:\t{1}", m.Author.Name.PadRight(16, ' '), m.Content);
                    }
                    suppressOutput = false;

                    //And now we send a message on our own
                    Console.WriteLine("Enter a message to send (or press enter to skip)");

                    suppressOutput = true;
                    string content = Console.ReadLine();
                    suppressOutput = false;

                    if (content != String.Empty)
                    {
                        await conversation.SendMessage(content);
                    }
                }

                // And now we send a message on our own
                Console.WriteLine("Enter a post to send (or press enter to skip)");

                suppressOutput = true;
                string post = Console.ReadLine();
                suppressOutput = false;

                if (post != String.Empty)
                {
                    bool result = await conn.SubmitPostAsync(post);

                    if(result)
                    {
                        Console.WriteLine("Post sent succesfully");
                    }
                    else
                    {
                        Console.WriteLine("Something went wrong");
                    }
                }


                //Finally we sign out.
                await conn.SignoffAsync();
                Console.WriteLine("Signed out");
                done = true;
            }
        }