diff --git a/HttpServer.cs b/HttpServer.cs index 03d06e2..a5c8449 100644 --- a/HttpServer.cs +++ b/HttpServer.cs @@ -271,7 +271,7 @@ namespace DualScreenDemo await HandleOrderSongListRequest(context); break; case "/message": - await HandlemessageRequest(context); + await HandleMessageRequest(context); break; case "/favorite": await HandleFavoriteRequest(context); @@ -858,7 +858,37 @@ namespace DualScreenDemo action(); } } - private static async Task HandlemessageRequest(HttpListenerContext context) + private static async Task HandleMessageRequest(HttpListenerContext context) + { + try + { + using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding)) + { + string json = await reader.ReadToEndAsync(); + if (!string.IsNullOrEmpty(json)) + { + context.Response.StatusCode = 200; + context.Response.ContentType = "application/json"; + int startIndex = json.IndexOf("\"message\":\"") + 11; + int endIndex = json.IndexOf("\"", startIndex); + if (startIndex >= 0 && endIndex >= 0) + OnDisplayBarrage?.Invoke( json.Substring(startIndex, endIndex - startIndex) ); + } + else + { + // 如果没有有效的 JSON 数据,返回错误响应 + context.Response.StatusCode = 400; // Bad Request + context.Response.StatusDescription = "Invalid JSON data."; + } + } + } + finally + { + context.Response.Close(); + } + } + /* + private static async Task HandleMessageRequest2(HttpListenerContext context) { try { @@ -878,17 +908,18 @@ namespace DualScreenDemo context.Response.ContentType = "application/json"; // 确保返回 JSON 格式 int startIndex = json.IndexOf("\"message\":\"") + 11; // 11 是 "message\":\"" 的长度 int endIndex = json.IndexOf("\"", startIndex); - string Messagefist ="藏鏡人:"; + string Messagefist = "藏鏡人:"; // 如果找到了 "message" 字段 if (startIndex >= 0 && endIndex >= 0) { // 提取 "message" 字段的值 string message = json.Substring(startIndex, endIndex - startIndex); - for(int i=0;i<3;i++) + for (int i = 0; i < 3; i++) { - string Messagelast=""; - for(int j=0;j < message.Length;j++){ + string Messagelast = ""; + for (int j = 0; j < message.Length; j++) + { Messagelast += message[j]; await Task.Delay(10); // 将读取到的 "message" 字段传递给 UI 控件显示 @@ -896,17 +927,17 @@ namespace DualScreenDemo { OverlayForm.MainForm.Invoke(new System.Action(() => { - OverlayForm.MainForm.ShowmessageLabel(Messagefist + Messagelast + '_'); + OverlayForm.MainForm.ShowMessageLabel(Messagefist + Messagelast + '_'); })); } else { - OverlayForm.MainForm.ShowmessageLabel(Messagefist + Messagelast + '_'); + OverlayForm.MainForm.ShowMessageLabel(Messagefist + Messagelast + '_'); } } // 真情告白顯示秒數 await Task.Delay(3000); - } + } } } else @@ -931,21 +962,9 @@ namespace DualScreenDemo { context.Response.Close(); } - } + }*/ - private static async Task OutputMessageAsync(string message, Label messageLabel) - { - messageLabel.Text = ""; // 清空现有的文本 - - foreach (char c in message) - { - messageLabel.Text += c; // 逐字显示消息 - await Task.Delay(500); // 每个字符间隔 0.5 秒 - } - - // 模拟等待 5 秒后继续其他操作 - await Task.Delay(5000); - } + private static string GetMimeType(string filePath) { diff --git a/OverlayFormObj/OverlayForm.Labels.cs b/OverlayFormObj/OverlayForm.Labels.cs index d8fa284..582403f 100644 --- a/OverlayFormObj/OverlayForm.Labels.cs +++ b/OverlayFormObj/OverlayForm.Labels.cs @@ -20,7 +20,7 @@ namespace OverlayFormObj private void InitializeLabels() - { + { InitializeBlackBackgroundPanel(); InitializeDisplayLabel(); InitializeDisplayLabels(); @@ -29,15 +29,11 @@ namespace OverlayFormObj InitializeTopRightLabel(); InitializeTopLeftLabel(); ConfigureKeyTimers(); - InitializemessageLabel(); } - + private void ConfigureKeyTimers() { - messageTimer.Interval = 5000; - messageTimer.Tick += (sender, e) => {messageLabel.Visible = false;messageTimer.Stop();RedisplayTopRigthLabel();}; - topRightTimer.Interval = 1000; topRightTimer.Tick += (sender, e) => { topRightLabel.Visible = false; topRightTimer.Stop(); RedisplayTopRigthLabel(); }; @@ -109,7 +105,7 @@ namespace OverlayFormObj marqueeTextSecondLine = ""; marqueeXPosSecondLine = this.Width; Invalidate(); - + } private void InitializeDisplayLabel() @@ -164,8 +160,8 @@ namespace OverlayFormObj pauseLabel.Location = new Point( (this.Width - pauseLabel.Width) / 2, // 水平居中 (this.Height - pauseLabel.Height) / 2 + 500 // 垂直居中偏下 50 像素 - ); - + ); + pauseLabel.Paint += (s, e) => { string text = "播放暫停"; @@ -214,11 +210,11 @@ namespace OverlayFormObj muteLabel.Location = new Point( (this.Width - muteLabel.Width) / 2, // 水平居中 (this.Height - muteLabel.Height) / 2 + 250 // 垂直居中偏下 50 像素 - ); - + ); + muteLabel.Paint += (s, e) => { - string text = "【全部靜音】"; + string text = "【全部靜音】"; Font font = muteLabel.Font; Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; @@ -282,7 +278,7 @@ namespace OverlayFormObj float yPosition2 = 56; Rectangle clipRect = new Rectangle( (int)(this.Width / 32), // 从1/8改成1/16(因为要居中) - (int)yPosition2, + (int)yPosition2, (int)(15 * this.Width / 16), // 从3/4改成7/8 (int)textSize.Height ); @@ -357,9 +353,9 @@ namespace OverlayFormObj }; } // 显示标准标签 - - public void ShowPauseLabel() => pauseLabel.Visible = true; + + public void ShowPauseLabel() => pauseLabel.Visible = true; // 隐藏暂停标签 public void HidePauseLabel() => pauseLabel.Visible = false; @@ -369,7 +365,7 @@ namespace OverlayFormObj // 隐藏静音标签 public void HideMuteLabel() => muteLabel.Visible = false; - public void ShowTopRightLabel(string customText = "",string tag =null) + public void ShowTopRightLabel(string customText = "", string tag = null) { if (tag != null) { @@ -379,7 +375,7 @@ namespace OverlayFormObj topRightLabel.Text = customText; topRightLabel.Visible = true; } - public void ShowTopRightLabelTime(string customText = "",string tag =null) + public void ShowTopRightLabelTime(string customText = "", string tag = null) { ShowTopRightLabel(customText, tag); topRightTimer.Stop(); @@ -401,37 +397,108 @@ namespace OverlayFormObj HideTopRightLabels(); ShowTopRightEchoLabel(switchmic); } - private void InitializemessageLabel() + public async void DisplayBarrage(string text) { - messageLabel = new Label(); - messageLabel.AutoSize = true; - messageLabel.ForeColor = Color.White; - messageLabel.Font = new Font("Microsoft JhengHei", 34, FontStyle.Bold); - messageLabel.BackColor = Color.Transparent; - messageLabel.Text = ""; - messageLabel.Visible = false; - this.Controls.Add(messageLabel); - UpdateMessageLabelPosition(); - this.Resize += (s, e) => UpdateMessageLabelPosition(); - } - public void ShowmessageLabel(string customText) - { - messageLabel.Visible = true; - messageLabel.Text = customText; - messageTimer.Start(); + if (this.InvokeRequired) + { + this.Invoke(new Action(() => DisplayBarrage(text))); + return; + } + + Label lblBarrage = new Label + { + Text = "藏鏡人:", // 一開始不顯示任何字 + AutoSize = true, + ForeColor = Color.White, + Font = new Font("Microsoft JhengHei", 34, FontStyle.Bold), + Location = new Point(10, this.Height / 2) + }; + + this.Controls.Add(lblBarrage); + lblBarrage.BringToFront(); + + // 逐字顯示文字 + for (int i = 0; i < text.Length; i++) + { + lblBarrage.Text += text[i]; + await Task.Delay(500); // 每個字間隔 200ms,可依喜好調整 + } + // 完成後停留 2 秒 + await Task.Delay(5000); + + // 閃爍三下 + for (int i = 0; i < 3; i++) + { + lblBarrage.Visible = false; + await Task.Delay(300); + lblBarrage.Visible = true; + await Task.Delay(300); + } + this.Controls.Remove(lblBarrage); + lblBarrage.Dispose(); } - // 隐藏標準調标签 - public void HidemessageLabel() + /* + public void DisplayBarrage1(string text) { - messageLabel.Visible = false; - messageTimer.Stop(); - } - private void UpdateMessageLabelPosition() - { - int yPosition = (this.Height - messageLabel.Height) / 2; - int xPosition = 10; - messageLabel.Location = new Point(xPosition, yPosition); + if (this.InvokeRequired) + { + this.Invoke(new System.Action(() => DisplayBarrage1(text))); + return; + } + + + Random rnd = new Random(); + + + for (int i = 0; i < 30; i++) + { + Label lblBarrage = new Label + { + Text = text, + AutoSize = true, + ForeColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)), + Font = new Font("Arial", rnd.Next(10, 50)), + Location = new Point(rnd.Next(0, this.Width), rnd.Next(0, this.Height)) + }; + + + this.Controls.Add(lblBarrage); + lblBarrage.BringToFront(); + + + System.Windows.Forms.Timer moveTimer = new System.Windows.Forms.Timer { Interval = 50 }; + moveTimer.Tick += (sender, e) => + { + lblBarrage.Left -= 5; + + if (lblBarrage.Right < 0) + { + lblBarrage.Dispose(); + moveTimer.Dispose(); + } + }; + moveTimer.Start(); + + + int duration = rnd.Next(3000, 7000); + System.Windows.Forms.Timer durationTimer = new System.Windows.Forms.Timer { Interval = duration }; + durationTimer.Tick += (sender, e) => + { + if (moveTimer.Enabled) + { + moveTimer.Stop(); + moveTimer.Dispose(); + } + + this.Controls.Remove(lblBarrage); + lblBarrage.Dispose(); + durationTimer.Stop(); + durationTimer.Dispose(); + }; + durationTimer.Start(); + } } + */ } } \ No newline at end of file diff --git a/OverlayFormObj/OverlayForm.cs b/OverlayFormObj/OverlayForm.cs index 673ac6b..36a2f48 100644 --- a/OverlayFormObj/OverlayForm.cs +++ b/OverlayFormObj/OverlayForm.cs @@ -29,8 +29,6 @@ namespace OverlayFormObj public static System.Timers.Timer unifiedTimer; private System.Windows.Forms.Timer stickerTimer1 = new System.Windows.Forms.Timer(); private System.Windows.Forms.Timer stickerTimer2 = new System.Windows.Forms.Timer(); - private System.Windows.Forms.Timer messageTimer = new System.Windows.Forms.Timer(); - public Label messageLabel; private System.Windows.Forms.Timer secondLineTimer; private DateTime secondLineStartTime; private const int secondLineDuration = 20000; @@ -620,69 +618,6 @@ namespace OverlayFormObj } - - - public void DisplayBarrage(string text) - { - if (this.InvokeRequired) - { - this.Invoke(new System.Action(() => DisplayBarrage(text))); - return; - } - - - Random rnd = new Random(); - - - for (int i = 0; i < 30; i++) - { - Label lblBarrage = new Label - { - Text = text, - AutoSize = true, - ForeColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)), - Font = new Font("Arial", rnd.Next(10, 50)), - Location = new Point(rnd.Next(0, this.Width), rnd.Next(0, this.Height)) - }; - - - this.Controls.Add(lblBarrage); - lblBarrage.BringToFront(); - - - System.Windows.Forms.Timer moveTimer = new System.Windows.Forms.Timer { Interval = 50 }; - moveTimer.Tick += (sender, e) => - { - lblBarrage.Left -= 5; - - if (lblBarrage.Right < 0) - { - lblBarrage.Dispose(); - moveTimer.Dispose(); - } - }; - moveTimer.Start(); - - - int duration = rnd.Next(3000, 7000); - System.Windows.Forms.Timer durationTimer = new System.Windows.Forms.Timer { Interval = duration }; - durationTimer.Tick += (sender, e) => - { - if (moveTimer.Enabled) - { - moveTimer.Stop(); - moveTimer.Dispose(); - } - - this.Controls.Remove(lblBarrage); - lblBarrage.Dispose(); - durationTimer.Stop(); - durationTimer.Dispose(); - }; - durationTimer.Start(); - } - } - public void DisplaySticker(string stickerId) { //Console.WriteLine("Attempting to display sticker."); diff --git a/Program.cs b/Program.cs index 6f543f8..d4ace41 100644 --- a/Program.cs +++ b/Program.cs @@ -17,7 +17,7 @@ namespace DualScreenDemo [STAThread] static void Main() { - Console.WriteLine("Server V.1.2.1 20250716"); + Console.WriteLine("Server V.1.2.1 202507171314"); if(Utils.Env.GetBool("IsCursor", true))Cursor.Hide(); AppDomain.CurrentDomain.ProcessExit += (s, e) => Cursor.Show(); //Console.WriteLine("正在與中控取得聯繫...");