コード例 #1
0
        private static void ReceiveHttp(Action <string> receiveAction, HttpLogger logger)
        {
            var client = new WebClient();

            while (!logger.IsDispose)
            {
                try
                {
                    var result = client.DownloadString(new Uri(logger.Url));
                    if (!string.IsNullOrEmpty(result))
                    {
                        var list = JsonConvert.DeserializeObject <List <LoggerModel> >(result);
                        foreach (var li in list)
                        {
                            receiveAction("**" + li.Msg);
                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                }
            }
        }
コード例 #2
0
        //private static UTF8Encoding encoding = new UTF8Encoding();

        public static async Task <ClientWebSocket> Connect(string uri, Action <string> receiveAction, HttpLogger loggerKey = null)
        {
            ClientWebSocket webSocket = null;

            try
            {
                webSocket = new System.Net.WebSockets.Managed.ClientWebSocket();
                await webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
                await ReceiveFirst(webSocket, receiveAction);

                new Task(async() =>
                {
                    try
                    {
                        await Receive(webSocket, receiveAction);
                    }
                    catch (Exception)
                    {
                        try
                        {
                            webSocket.Dispose();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }).Start();

                new Task(() =>
                {
                    try
                    {
                        ReceiveHttp(receiveAction, loggerKey);
                    }
                    catch (Exception)
                    {
                    }
                }).Start();
            }
            catch (Exception ex)
            {
                //ignore
            }

            return(webSocket);
        }