websocket_fasthttp/socket_handlers.go

117 lines
2.9 KiB
Go
Raw Normal View History

2017-11-07 10:03:40 +00:00
package websocket_fasthttp
import (
2017-11-08 10:15:09 +00:00
"time"
2017-11-07 10:03:40 +00:00
"github.com/valyala/fasthttp"
)
type SocketHandlers struct {
2017-11-08 10:15:09 +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-10 08:33:40 +00:00
2017-11-10 16:49:00 +00:00
sockets map[string]Socket
2017-11-10 08:33:40 +00:00
}
2017-11-27 11:19:19 +00:00
func (sh *SocketHandlers) Init(serverCTX ServerContext) error {
2017-11-10 16:49:00 +00:00
sh.sockets = make(map[string]Socket)
2017-11-10 08:33:40 +00:00
return nil
2017-11-07 10:03:40 +00:00
}
2017-11-27 14:30:18 +00:00
func (sh *SocketHandlers) Handshake(serverCTX ServerContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) {
2017-11-10 08:33:40 +00:00
return "", nil
2017-11-07 10:03:40 +00:00
}
2017-11-10 08:33:40 +00:00
2017-11-27 14:30:18 +00:00
func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext {
return newSocketContext(serverCTX)
}
2017-11-10 16:49:00 +00:00
func (sh *SocketHandlers) OnConnect(soc Socket) Socket {
2017-11-14 05:21:09 +00:00
2017-11-10 16:49:00 +00:00
return soc
2017-11-09 09:47:30 +00:00
}
2017-11-08 05:30:56 +00:00
2017-11-10 16:49:00 +00:00
func (sh *SocketHandlers) Handle(soc Socket, stopChan <-chan struct{}, doneChan chan<- struct{}) {
2017-11-07 10:03:40 +00:00
// no op
}
2017-11-10 16:49:00 +00:00
func (sh *SocketHandlers) OnDisconnect(soc Socket) {
2017-11-14 05:21:09 +00:00
2017-11-10 08:33:40 +00:00
}
func (sh *SocketHandlers) Destroy() {
2017-11-09 09:47:30 +00:00
// no op
}
2017-11-10 16:49:00 +00:00
func (sh *SocketHandlers) GetSocket(id string) Socket {
2017-11-10 08:33:40 +00:00
return sh.sockets[id]
}
2017-11-10 16:49:00 +00:00
func (sh *SocketHandlers) GetSockets() map[string]Socket {
2017-11-10 08:33:40 +00:00
return sh.sockets
}
2017-11-08 10:15:09 +00:00
func (sh *SocketHandlers) GetMaxMessageSize() int64 {
return sh.MaxMessageSize
}
func (sh *SocketHandlers) GetWriteTimeout() time.Duration {
return sh.WriteTimeout
}
func (sh *SocketHandlers) GetReadTimeout() time.Duration {
return sh.ReadTimeout
}
func (sh *SocketHandlers) GetPongTimeout() time.Duration {
return sh.PongTimeout
}
func (sh *SocketHandlers) GetPingTimeout() time.Duration {
return sh.PingTimeout
}
func (sh *SocketHandlers) GetPingPeriod() time.Duration {
return sh.PingPeriod
}
2017-11-07 10:03:40 +00:00
2017-11-08 10:15:09 +00:00
func (sh *SocketHandlers) Validate() {
if sh.MaxMessageSize <= 0 {
sh.MaxMessageSize = DefaultMaxMessageSize
}
if sh.WriteTimeout <= 0 {
sh.WriteTimeout = DefaultWriteTimeout
}
if sh.ReadTimeout <= 0 {
sh.ReadTimeout = DefaultReadTimeout
}
if sh.PongTimeout <= 0 {
sh.PongTimeout = DefaultPongTimeout
}
if sh.PingTimeout <= 0 {
sh.PingTimeout = DefaultPingTimeout
}
if sh.PingPeriod <= 0 {
sh.PingPeriod = DefaultPingPeriod
}
2017-11-07 10:03:40 +00:00
}
2017-11-14 05:21:09 +00:00
func (sh *SocketHandlers) addSocket(soc Socket) {
sh.sockets[soc.ID()] = soc
}
func (sh *SocketHandlers) removeSocket(soc Socket) {
delete(sh.sockets, soc.ID())
}