Skip to content

Commit ccc8504

Browse files
committed
feat(网络): 添加应用获得焦点时发送心跳包的功能
添加 SetFocusHeartbeat 方法控制网络频道在应用获得焦点时是否发送心跳包 在 NetworkComponent 中添加 m_FocusHeartbeat 配置项和 OnApplicationFocus 回调 更新 NetworkChannelBase 实现心跳包发送逻辑的条件判断
1 parent 688de90 commit ccc8504

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

Editor/Inspector/NetworkComponentInspector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ internal sealed class NetworkComponentInspector : ComponentTypeComponentInspecto
4343
private SerializedProperty m_IgnoredSendNetworkIds;
4444
private SerializedProperty m_IgnoredReceiveNetworkIds;
4545
private SerializedProperty m_rpcTimeout;
46+
private SerializedProperty m_FocusHeartbeat;
4647

4748
private readonly GUIContent m_IgnoredSendNetworkIdsGUIContent = new GUIContent("Ignore Log Printing For Sent Message IDs");
4849
private readonly GUIContent m_IgnoredReceiveNetworkIdsGUIContent = new GUIContent("Ignore Log Printing Of Received Message IDs");
4950
private readonly GUIContent m_rpcTimeoutGUIContent = new GUIContent("RPC Timeout Time In Milliseconds");
51+
private readonly GUIContent m_FocusHeartbeatGUIContent = new GUIContent("Focus Send Heartbeat");
5052

5153
public override void OnInspectorGUI()
5254
{
@@ -56,6 +58,7 @@ public override void OnInspectorGUI()
5658
{
5759
GUI.enabled = !EditorApplication.isPlaying;
5860
EditorGUILayout.IntSlider(m_rpcTimeout, 3000, 50000, m_rpcTimeoutGUIContent);
61+
EditorGUILayout.PropertyField(m_FocusHeartbeat, m_FocusHeartbeatGUIContent);
5962
EditorGUILayout.PropertyField(m_IgnoredSendNetworkIds, m_IgnoredSendNetworkIdsGUIContent);
6063
EditorGUILayout.PropertyField(m_IgnoredReceiveNetworkIds, m_IgnoredReceiveNetworkIdsGUIContent);
6164
GUI.enabled = false;
@@ -121,6 +124,7 @@ protected override void Enable()
121124
m_IgnoredSendNetworkIds = serializedObject.FindProperty("m_IgnoredSendNetworkIds");
122125
m_IgnoredReceiveNetworkIds = serializedObject.FindProperty("m_IgnoredReceiveNetworkIds");
123126
m_rpcTimeout = serializedObject.FindProperty("m_rpcTimeout");
127+
m_FocusHeartbeat = serializedObject.FindProperty("m_FocusHeartbeat");
124128
}
125129
}
126130
}

Runtime/Network/Interface/INetworkManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,11 @@ public interface INetworkManager
105105
/// <param name="channelName">网络频道名称。</param>
106106
/// <returns>是否销毁网络频道成功。</returns>
107107
bool DestroyNetworkChannel(string channelName);
108+
109+
/// <summary>
110+
/// 设置是否在应用程序获得焦点时发送心跳包。
111+
/// </summary>
112+
/// <param name="hasFocus">是否在应用程序获得焦点时发送心跳包。</param>
113+
void SetFocusHeartbeat(bool hasFocus);
108114
}
109115
}

Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public abstract class NetworkChannelBase : INetworkChannel, IDisposable
115115
/// </summary>
116116
protected int PReceivedPacketCount;
117117

118+
/// <summary>
119+
/// 是否在应用程序获得焦点时发送心跳包
120+
/// </summary>
121+
protected bool PFocusHeartbeat;
122+
118123
/// <summary>
119124
/// 是否正在连接中
120125
/// </summary>
@@ -179,6 +184,7 @@ public NetworkChannelBase(string name, INetworkChannelHelper networkChannelHelpe
179184
PSentPacketCount = 0;
180185
PReceivedPacketCount = 0;
181186
PActive = false;
187+
PFocusHeartbeat = true;
182188
PIsConnecting = false;
183189
m_Disposed = false;
184190
NetworkChannelConnected = null;
@@ -449,15 +455,18 @@ private void ProcessHeartBeat(float realElapseSeconds)
449455
PHeartBeatState.MissHeartBeatCount++;
450456
}
451457

452-
if (sendHeartBeat && PNetworkChannelHelper.SendHeartBeat())
458+
if (sendHeartBeat && PFocusHeartbeat)
453459
{
454-
if (missHeartBeatCount > 0 && NetworkChannelMissHeartBeat != null)
460+
if (PNetworkChannelHelper.SendHeartBeat())
455461
{
456-
NetworkChannelMissHeartBeat(this, missHeartBeatCount);
457-
}
462+
if (missHeartBeatCount > 0 && NetworkChannelMissHeartBeat != null)
463+
{
464+
NetworkChannelMissHeartBeat(this, missHeartBeatCount);
465+
}
458466

459-
// PHeartBeatState.Reset(this.ResetHeartBeatElapseSecondsWhenReceivePacket);
460-
return;
467+
// PHeartBeatState.Reset(this.ResetHeartBeatElapseSecondsWhenReceivePacket);
468+
return;
469+
}
461470
}
462471

463472
if (PHeartBeatState.MissHeartBeatCount > MissHeartBeatCountByClose)
@@ -967,6 +976,11 @@ public void SetIgnoreLogNetworkIds(List<int> sendIds, List<int> receiveIds)
967976
IgnoreSendIds = sendIds;
968977
IgnoreReceiveIds = receiveIds;
969978
}
979+
980+
public void SetFocusHeartbeat(bool hasFocus)
981+
{
982+
PFocusHeartbeat = hasFocus;
983+
}
970984
}
971985
}
972986
}

Runtime/Network/Network/NetworkManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ public bool DestroyNetworkChannel(string channelName)
246246
return false;
247247
}
248248

249+
public void SetFocusHeartbeat(bool hasFocus)
250+
{
251+
foreach (var networkChannel in m_NetworkChannels.Values)
252+
{
253+
networkChannel.SetFocusHeartbeat(hasFocus);
254+
}
255+
}
256+
249257
private void OnNetworkChannelConnected(NetworkChannelBase networkChannel, object userData)
250258
{
251259
if (m_NetworkConnectedEventHandler != null)

Runtime/Network/NetworkComponent.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public sealed class NetworkComponent : GameFrameworkComponent
6262
/// </summary>
6363
[SerializeField] private int m_rpcTimeout = 5000;
6464

65+
/// <summary>
66+
/// 是否在应用程序获得焦点时发送心跳包,默认为true
67+
/// </summary>
68+
[SerializeField] private bool m_FocusHeartbeat = true;
69+
6570
/// <summary>
6671
/// 获取网络频道数量。
6772
/// </summary>
@@ -70,6 +75,22 @@ public int NetworkChannelCount
7075
get { return m_NetworkManager.NetworkChannelCount; }
7176
}
7277

78+
/// <summary>
79+
/// 是否正在游戏中。
80+
/// </summary>
81+
private bool _isGaming = true;
82+
83+
/// <summary>
84+
/// 应用程序获得或失去焦点时调用。
85+
/// </summary>
86+
/// <param name="hasFocus">应用程序是否获得焦点。</param>
87+
private void OnApplicationFocus(bool hasFocus)
88+
{
89+
_isGaming = hasFocus;
90+
m_NetworkManager.SetFocusHeartbeat(hasFocus);
91+
Log.Info($"SetFocusHeartbeat: {hasFocus}");
92+
}
93+
7394
/// <summary>
7495
/// 游戏框架组件初始化。
7596
/// </summary>

0 commit comments

Comments
 (0)