Exemplo n.º 1
0
        private static async Task <LPServerInfo> GetServer()
        {
            try
            {
                _server = (await API.Call <LPServerInfo>("messages.getLongPollServer")).Response;

                return(_server);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), e.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Console.WriteLine(e.StackTrace);
            }

            return(await GetServer());
        }
Exemplo n.º 2
0
        public static void Connect(bool failed = false)
        {
            new Thread(async() =>
            {
                try
                {
                    while (true)
                    {
                        while (_server == null || failed)
                        {
                            _server = await GetServer();
                            failed  = false;
                        }

                        String u = "http://" + _server.Server + "?act=a_check&key=" + _server.Key + "&ts=" + _server.TS +
                                   "&wait=25&mode=2";

                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(u);

                        WebResponse r = await req.GetResponseAsync();

                        Stream s = r.GetResponseStream();

                        if (s != null)
                        {
                            StreamReader sr = new StreamReader(s);

                            String str = sr.ReadToEnd();

                            if (!String.IsNullOrEmpty(str))
                            {
                                LPServerResponse resp = JsonConvert.DeserializeObject <LPServerResponse>(str);

                                if (resp.Failed != -1)
                                {
                                    failed = true;
                                }
                                else
                                {
                                    _server.TS = resp.TS;

                                    foreach (JArray upd in resp.Updates)
                                    {
                                        LPEvent e = new LPEvent(upd.Cast <object>().ToArray());

                                        Console.WriteLine(e.EventType + @": " + String.Join(", ", e.Data));

                                        if (Events.ContainsKey(e.EventType))
                                        {
                                            Events[e.EventType].Dispatch(e);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString(), e.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }).Start();
        }