server-go/socket/client-conn-handler.go

56 lines
1.2 KiB
Go
Raw Normal View History

2018-04-05 16:21:13 +00:00
package socket
2018-04-04 13:28:35 +00:00
import (
"time"
2018-04-05 16:21:13 +00:00
"git.loafle.net/commons/server-go"
2018-04-04 13:28:35 +00:00
)
2018-04-04 13:50:34 +00:00
type ClientConnHandler interface {
2018-04-05 16:21:13 +00:00
server.ConnectionHandler
2018-04-04 13:28:35 +00:00
GetReconnectInterval() time.Duration
GetReconnectTryTime() int
}
2018-04-04 13:50:34 +00:00
type ClientConnHandlers struct {
2018-04-05 16:21:13 +00:00
server.ConnectionHandlers
2018-04-04 13:28:35 +00:00
2018-04-12 05:55:01 +00:00
ReconnectInterval time.Duration `json:"reconnectInterval"`
ReconnectTryTime int `json:"reconnectTryTime"`
2018-04-04 13:28:35 +00:00
}
2018-04-04 13:50:34 +00:00
func (cch *ClientConnHandlers) GetReconnectInterval() time.Duration {
2018-04-04 13:28:35 +00:00
return cch.ReconnectInterval
}
2018-04-04 13:50:34 +00:00
func (cch *ClientConnHandlers) GetReconnectTryTime() int {
2018-04-04 13:28:35 +00:00
return cch.ReconnectTryTime
}
2018-04-12 05:55:01 +00:00
func (cch *ClientConnHandlers) Clone() *ClientConnHandlers {
return &ClientConnHandlers{
ConnectionHandlers: *cch.ConnectionHandlers.Clone(),
ReconnectInterval: cch.ReconnectInterval,
ReconnectTryTime: cch.ReconnectTryTime,
}
}
2018-04-04 13:50:34 +00:00
func (cch *ClientConnHandlers) Validate() error {
2018-04-04 13:28:35 +00:00
if err := cch.ConnectionHandlers.Validate(); nil != err {
return err
}
if cch.ReconnectInterval <= 0 {
2018-04-05 16:21:13 +00:00
cch.ReconnectInterval = server.DefaultReconnectInterval
2018-04-13 06:37:27 +00:00
} else {
cch.ReconnectInterval = cch.ReconnectInterval * time.Second
2018-04-04 13:28:35 +00:00
}
if cch.ReconnectTryTime <= 0 {
2018-04-05 16:21:13 +00:00
cch.ReconnectTryTime = server.DefaultReconnectTryTime
2018-04-04 13:28:35 +00:00
}
return nil
}