websocket_fasthttp/client/socket_handlers.go

82 lines
2.2 KiB
Go
Raw Permalink Normal View History

2017-11-29 12:24:48 +00:00
package client
import (
2017-11-30 01:11:53 +00:00
"net/http"
2017-11-29 12:24:48 +00:00
"time"
2017-11-30 01:11:53 +00:00
2017-11-30 03:15:05 +00:00
cwf "git.loafle.net/commons_go/websocket_fasthttp"
2017-11-29 12:24:48 +00:00
)
type SocketHandlers struct {
2017-11-30 03:15:05 +00:00
// MaxMessageSize is the maximum size for a message read from the peer. If a
// message exceeds the limit, the connection sends a close frame to the peer
// and returns ErrReadLimit to the application.
MaxMessageSize int64
// WriteTimeout is the write deadline on the underlying network
// connection. After a write has timed out, the websocket state is corrupt and
// all future writes will return an error. A zero value for t means writes will
// not time out.
WriteTimeout time.Duration
// ReadTimeout is the read deadline on the underlying network connection.
// After a read has timed out, the websocket connection state is corrupt and
// all future reads will return an error. A zero value for t means reads will
// not time out.
ReadTimeout time.Duration
PongTimeout time.Duration
PingTimeout time.Duration
PingPeriod time.Duration
2017-11-30 01:11:53 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) OnConnect(socketContext SocketContext, res *http.Response) {
// no op
2017-11-30 01:11:53 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) OnDisconnect(soc Socket) {
// no op
2017-11-29 12:24:48 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) GetMaxMessageSize() int64 {
return sh.MaxMessageSize
2017-11-29 12:24:48 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) GetWriteTimeout() time.Duration {
return sh.WriteTimeout
2017-11-29 12:24:48 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) GetReadTimeout() time.Duration {
return sh.ReadTimeout
2017-11-30 01:11:53 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) GetPongTimeout() time.Duration {
return sh.PongTimeout
2017-11-30 01:11:53 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) GetPingTimeout() time.Duration {
return sh.PingTimeout
2017-11-30 01:11:53 +00:00
}
2017-11-30 03:15:05 +00:00
func (sh *SocketHandlers) GetPingPeriod() time.Duration {
return sh.PingPeriod
2017-11-29 12:24:48 +00:00
}
func (sh *SocketHandlers) Validate() {
2017-11-30 03:15:05 +00:00
if sh.MaxMessageSize <= 0 {
sh.MaxMessageSize = cwf.DefaultMaxMessageSize
}
if sh.WriteTimeout <= 0 {
sh.WriteTimeout = cwf.DefaultWriteTimeout
}
if sh.ReadTimeout <= 0 {
sh.ReadTimeout = cwf.DefaultReadTimeout
}
if sh.PongTimeout <= 0 {
sh.PongTimeout = cwf.DefaultPongTimeout
}
if sh.PingTimeout <= 0 {
sh.PingTimeout = cwf.DefaultPingTimeout
}
if sh.PingPeriod <= 0 {
sh.PingPeriod = cwf.DefaultPingPeriod
}
}
2017-11-30 01:11:53 +00:00
2017-11-30 03:15:05 +00:00
func NewSocketHandler() SocketHandler {
return &SocketHandlers{}
2017-11-29 12:24:48 +00:00
}