C
C
continue2015-10-26 17:52:16
C++ / C#
continue, 2015-10-26 17:52:16

Program in C# + VK API?

Good day! There is a certain program that receives messages through the messages.get method (vk api), I would like to know how to make the program work in real time?
Examples with code are welcome.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mark Doe, 2015-10-26
@mourr

The easiest option - using the Timer class to make a small background timer with a small interval - you will receive messages with an acceptable delay of 400-600ms

C
continue, 2015-10-26
@continue

It is necessary to loop the execution of the program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication14
{
    class Program
    {
        private static System.Timers.Timer aTimer;
        public static void Main()
        {
            /*string link_in_pogoda = "http://export.yandex.ru/weather-ng/forecasts/29642.xml";
            string resultPage_pogoda = "";
            HttpWebRequest request_pogoda = (HttpWebRequest)WebRequest.Create(link_in_pogoda);
            HttpWebResponse response_pogoda = (HttpWebResponse)request_pogoda.GetResponse();
            Encoding Code_pogoda = Encoding.GetEncoding(1251);
            using (StreamReader sr_pogoda = new StreamReader(response_pogoda.GetResponseStream(), Encoding.UTF8, true))
            {
                resultPage_pogoda = sr_pogoda.ReadToEnd();
                sr_pogoda.Close();
            }
            Console.WriteLine(resultPage_pogoda); */
            while (true)
            {
                aTimer = new System.Timers.Timer();
                aTimer.Interval = 600;
                aTimer = new System.Timers.Timer(2000);
                aTimer.Enabled = true;
                aTimer.Elapsed += OnTimedEvent;
                Console.ReadLine();
            }
        }
        private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {

            string link = protocol + url + method + count + ampersand + token + ampersand + expires_in + ampersand + user_id + ampersand + revoke;
            string resultPage = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Encoding Code = Encoding.GetEncoding(1251);
            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true))
            {
                resultPage = sr.ReadToEnd();
                sr.Close();
            }
            JObject testObject = JObject.Parse(resultPage);
            string body = "";
            int id_vk = -1;
            foreach (JToken j in testObject["response"])
            {
                if (j is JObject)
                {
                    body = (string)j["body"];
                    id_vk = (int)j["uid"];
                }
            }
            if (body.Contains("погод"))
            {
                const string protocol_send_messenge = "https://";
                const string url_send_messenge = "api.vkontakte.ru/method/";
                string user_id_ = "user_id=" + id_vk;
                const string method_send_messenge = "messages.send?";
                const string messenge = "message=тестинг";
                string link_send_messenge = protocol_send_messenge + url_send_messenge + method_send_messenge;
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(link_send_messenge);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";

                byte[] SomeBytes = null;
                string FormParams = user_id_ + ampersand + messenge + ampersand + token;
                SomeBytes = Encoding.UTF8.GetBytes(FormParams);
                req.ContentLength = SomeBytes.Length;
                Stream newStream = req.GetRequestStream();
                newStream.Write(SomeBytes, 0, SomeBytes.Length);
            }
        }
    }
}

A
Alex493049469, 2015-10-29
@Alex493049469

Wrong question, leads to not quite correct answers. Read VK API about long pull server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question