From abdb761b8c86332b6b513477a6867d384364ebe4 Mon Sep 17 00:00:00 2001 From: lxmou666 <772765102@qq.com> Date: Sun, 14 Mar 2021 19:19:04 +0800 Subject: [PATCH] =?UTF-8?q?ws=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JianGongYun/TRTC/LiveClassroom.cs | 9 ++-- JianGongYun/TRTC/Utils/Util.cs | 52 ++++++++++++++++++++- JianGongYun/TRTC/Windows/LiveWindow.xaml.cs | 2 + 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/JianGongYun/TRTC/LiveClassroom.cs b/JianGongYun/TRTC/LiveClassroom.cs index 6c5075b..0d78985 100644 --- a/JianGongYun/TRTC/LiveClassroom.cs +++ b/JianGongYun/TRTC/LiveClassroom.cs @@ -146,16 +146,18 @@ namespace JianGongYun.TRTC WSClient.ReconnectTimeout = TimeSpan.FromSeconds(10); WSClient.ReconnectionHappened.Subscribe((info) => { - Console.WriteLine($"Reconnection happened, type: {info.Type}"); + $"Reconnection happened, type: {info.Type}".ToLocalLog(); if (WSClient.IsRunning) { - WSClient.Send($"{{code:10000,nick:\"{CurrentClassroomEntity.TeacherName}\",\"room\":\"push_{ClassId}\"}}");//进房间 + var joinRoom = $"{{code:10000,nick:\"{CurrentClassroomEntity.TeacherName}\",\"room\":\"push_{ClassId}\"}}"; + WSClient.Send(joinRoom);//进房间 + joinRoom.ToLocalLog(); } liveWinMode.WSConneced = true; }); WSClient.DisconnectionHappened.Subscribe((info) => { - Console.WriteLine($"DisconnectionHappened happened, type: {info.Type}, {info.CloseStatusDescription}"); + $"DisconnectionHappened happened, type: {info.Type} {info.CloseStatusDescription} {info.Exception}".ToLocalLog(); liveWinMode.WSConneced = false; }); WSClient.MessageReceived.Subscribe(CurrentLiveWindow.OnMsg); @@ -170,6 +172,7 @@ namespace JianGongYun.TRTC if (WSClient.IsRunning) { WSClient.Send("{code:10016}");//向服务器发送心跳包 + "发送心跳包 {code:10016}".ToLocalLog(); } }); Heartbeat.Start(); diff --git a/JianGongYun/TRTC/Utils/Util.cs b/JianGongYun/TRTC/Utils/Util.cs index 58be87e..65868ca 100644 --- a/JianGongYun/TRTC/Utils/Util.cs +++ b/JianGongYun/TRTC/Utils/Util.cs @@ -7,6 +7,8 @@ using System.Security.Cryptography; using System.Text; using System.Windows.Media; using System.Windows.Media.Imaging; +using System.Threading.Channels; +using System.Threading.Tasks; namespace JianGongYun.TRTC.Utils { @@ -53,7 +55,7 @@ namespace JianGongYun.TRTC.Utils public static bool IsTestEnv() { - string path =Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ShowTestEnv.txt"); + string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ShowTestEnv.txt"); return File.Exists(path); } @@ -91,5 +93,53 @@ namespace JianGongYun.TRTC.Utils } public static string TimeStr() => DateTime.Now.ToString("yyyyMMddHHmmssfff"); + + private static readonly Channel _logQueue = Channel.CreateUnbounded(new UnboundedChannelOptions() + { + SingleReader = true, + SingleWriter = false + }); + static Util() + { + string log = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs"); + if (!System.IO.Directory.Exists(log)) + { + Directory.CreateDirectory(log); + } + log = Path.Combine(log, $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.log"); + Task.Factory.StartNew(async () => + { + try + { + while (await _logQueue.Reader.WaitToReadAsync()) + { + while (_logQueue.Reader.TryRead(out var message)) + { + try + { + await System.IO.File.AppendAllTextAsync(log, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}--------------------------{Environment.NewLine}{message}{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}"); + } + catch (Exception e) + { + e.Message.ToLocalLog(); + } + } + } + } + catch (Exception ex) + { + ex.Message.ToLocalLog(); + } + }, TaskCreationOptions.LongRunning); + } + + public static void ToLocalLog(this string val) + { + if (!string.IsNullOrWhiteSpace(val)) + { + Console.WriteLine(val); + _logQueue.Writer.TryWrite(val); + } + } } } diff --git a/JianGongYun/TRTC/Windows/LiveWindow.xaml.cs b/JianGongYun/TRTC/Windows/LiveWindow.xaml.cs index b8fbae1..4d5f20d 100644 --- a/JianGongYun/TRTC/Windows/LiveWindow.xaml.cs +++ b/JianGongYun/TRTC/Windows/LiveWindow.xaml.cs @@ -19,6 +19,7 @@ using System.ComponentModel; using Websocket.Client; using Newtonsoft.Json.Linq; using System.Diagnostics; +using JianGongYun.TRTC.Utils; namespace JianGongYun.TRTC.Windows { @@ -91,6 +92,7 @@ namespace JianGongYun.TRTC.Windows })); } } + $"心跳包回应 {msg.Text}".ToLocalLog(); break; } return;