增加心跳

This commit is contained in:
2025-09-09 10:51:36 +08:00
parent 8b22514aad
commit fc657d7448
6 changed files with 211 additions and 8 deletions

View File

@@ -20,6 +20,9 @@ type Config struct {
// WebSocket WebSocket配置
WebSocket WebSocketConfig `yaml:"websocket"`
// Heartbeat 心跳配置
Heartbeat HeartbeatConfig `yaml:"heartbeat"`
}
// ServerConfig 代表服务器配置
@@ -76,12 +79,21 @@ type WebSocketConfig struct {
Timeout int `yaml:"timeout"`
}
// HeartbeatConfig 代表心跳配置
type HeartbeatConfig struct {
// Interval 心跳间隔(秒)
Interval int `yaml:"interval"`
}
// NewConfig 创建并返回一个新的配置实例
func NewConfig() *Config {
return &Config{
WebSocket: WebSocketConfig{
Timeout: 5, // 默认5秒超时
},
Heartbeat: HeartbeatConfig{
Interval: 30, // 默认30秒心跳间隔
},
}
}
@@ -122,3 +134,11 @@ func (c *Config) GetWebSocketTimeout() int {
}
return c.WebSocket.Timeout
}
// GetHeartbeatInterval 获取心跳间隔(秒)
func (c *Config) GetHeartbeatInterval() int {
if c.Heartbeat.Interval <= 0 {
return 30 // 默认30秒心跳间隔
}
return c.Heartbeat.Interval
}