新增心跳封包模組
This commit is contained in:
parent
91fad6df31
commit
fcd901c613
39
HeartbeatSender.cs
Normal file
39
HeartbeatSender.cs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Timers;
|
||||||
|
namespace HeartbeatSender{
|
||||||
|
public class heartbeatSender
|
||||||
|
{
|
||||||
|
private readonly UdpClient udpClient;
|
||||||
|
private readonly IPEndPoint remoteEndPoint;
|
||||||
|
private readonly System.Timers.Timer heartbeatTimer;
|
||||||
|
public IPEndPoint RemoteEndPoint => remoteEndPoint;
|
||||||
|
public heartbeatSender(string targetIp, int targetPort, int intervalMilliseconds = 3000)
|
||||||
|
{
|
||||||
|
udpClient = new UdpClient();
|
||||||
|
// 設置 IP 和 PORT
|
||||||
|
remoteEndPoint = new IPEndPoint(IPAddress.Parse(targetIp), targetPort);
|
||||||
|
heartbeatTimer = new System.Timers.Timer(intervalMilliseconds);
|
||||||
|
heartbeatTimer.Elapsed += SendHeartbeat;
|
||||||
|
heartbeatTimer.AutoReset = true;
|
||||||
|
}
|
||||||
|
private void SendHeartbeat(object sender, ElapsedEventArgs e)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string heartbeatMessage = "HEARTBEAT";
|
||||||
|
byte[] data = Encoding.UTF8.GetBytes(heartbeatMessage);
|
||||||
|
udpClient.Send(data, data.Length, remoteEndPoint);
|
||||||
|
Console.WriteLine($"Heartbeat sent to {remoteEndPoint.Address}:{remoteEndPoint.Port}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error sending heartbeat: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void Start() => heartbeatTimer.Start();
|
||||||
|
public void Stop() => heartbeatTimer.Stop();
|
||||||
|
}
|
||||||
|
}
|
@ -385,13 +385,13 @@ namespace DualScreenDemo
|
|||||||
// 若播放公播歌單,代表已點歌曲皆已播放完畢
|
// 若播放公播歌單,代表已點歌曲皆已播放完畢
|
||||||
hasBeenPlayed = true;
|
hasBeenPlayed = true;
|
||||||
songLabel.ForeColor = Color.Gray;
|
songLabel.ForeColor = Color.Gray;
|
||||||
statusText = IsSimplified ? "(播毕)" : "(播畢播畢)";
|
statusText = IsSimplified ? "(播毕)" : "(播畢)";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 計算已完成播放的歌曲數量
|
// 計算已完成播放的歌曲數量
|
||||||
int completedCount = currentSongIndexInHistory;
|
int completedCount = currentSongIndexInHistory;
|
||||||
Console.WriteLine("currentSongIndexInHistory:" + currentSongIndexInHistory);
|
//Console.WriteLine("currentSongIndexInHistory:" + currentSongIndexInHistory);
|
||||||
// 遍歷已點歌曲歷史
|
// 遍歷已點歌曲歷史
|
||||||
/*for (int i = 0; i <= currentSongIndexInHistory && i < playedSongsHistory.Count; i++) {
|
/*for (int i = 0; i <= currentSongIndexInHistory && i < playedSongsHistory.Count; i++) {
|
||||||
if (i == currentSongIndexInHistory) {
|
if (i == currentSongIndexInHistory) {
|
||||||
@ -414,14 +414,14 @@ namespace DualScreenDemo
|
|||||||
// 已播放完成
|
// 已播放完成
|
||||||
hasBeenPlayed = true;
|
hasBeenPlayed = true;
|
||||||
songLabel.ForeColor = Color.Gray;
|
songLabel.ForeColor = Color.Gray;
|
||||||
statusText = IsSimplified ? "(播毕)" : "(播畢播畢)";
|
statusText = IsSimplified ? "(播毕)" : "(播畢)";
|
||||||
}
|
}
|
||||||
else if (songPosition == completedCount)
|
else if (songPosition == completedCount)
|
||||||
{
|
{
|
||||||
// 正在播放
|
// 正在播放
|
||||||
isCurrentlyPlaying = true;
|
isCurrentlyPlaying = true;
|
||||||
songLabel.ForeColor = Color.LimeGreen;
|
songLabel.ForeColor = Color.LimeGreen;
|
||||||
statusText = IsSimplified ? "(播放中)" : "(播放中播放中)";
|
statusText = IsSimplified ? "(播放中)" : "(播放中)";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -4,8 +4,8 @@ using System.Text;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.IO; // 為 Path 和 File 提供支持
|
using System.IO; // 為 Path 和 File 提供支持
|
||||||
using DBObj;
|
using DBObj;
|
||||||
using System.Diagnostics;
|
|
||||||
using OverlayFormObj;
|
using OverlayFormObj;
|
||||||
|
using HeartbeatSender;
|
||||||
namespace DualScreenDemo
|
namespace DualScreenDemo
|
||||||
{
|
{
|
||||||
public class TCPServer
|
public class TCPServer
|
||||||
@ -82,6 +82,10 @@ namespace DualScreenDemo
|
|||||||
{
|
{
|
||||||
listener.Start();
|
listener.Start();
|
||||||
Console.WriteLine("Server started on port " + Port + ".");
|
Console.WriteLine("Server started on port " + Port + ".");
|
||||||
|
// 心跳封包
|
||||||
|
//heartbeatSender sender = new heartbeatSender("127.0.0.1", 8888);
|
||||||
|
//sender.Start();
|
||||||
|
// Console.WriteLine($"heart beat for server{sender.RemoteEndPoint.Address}:{sender.RemoteEndPoint.Port}");
|
||||||
try {
|
try {
|
||||||
string stateFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"txt","states.txt");
|
string stateFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"txt","states.txt");
|
||||||
string initialState = ReadStateFile(stateFilePath);
|
string initialState = ReadStateFile(stateFilePath);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user