server/socket_handlers.go

94 lines
2.3 KiB
Go
Raw Permalink Normal View History

2017-11-29 00:51:41 +00:00
package server
import (
"net"
"time"
)
type SocketHandlers struct {
// 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
sockets map[string]Socket
}
2017-12-01 08:26:02 +00:00
func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext {
return newSocketContext(serverCTX)
}
2017-11-29 00:51:41 +00:00
func (sh *SocketHandlers) Init(serverCTX ServerContext) error {
sh.sockets = make(map[string]Socket)
return nil
}
2017-12-01 08:26:02 +00:00
func (sh *SocketHandlers) Handshake(socketCTX SocketContext, conn net.Conn) (id string) {
2017-11-29 03:46:40 +00:00
return ""
2017-11-29 00:51:41 +00:00
}
func (sh *SocketHandlers) OnConnect(soc Socket) {
// no op
}
func (sh *SocketHandlers) Handle(soc Socket, stopChan <-chan struct{}, doneChan chan<- error) {
// no op
}
func (sh *SocketHandlers) OnDisconnect(soc Socket) {
}
func (sh *SocketHandlers) Destroy() {
// no op
}
func (sh *SocketHandlers) GetSocket(id string) Socket {
return sh.sockets[id]
}
2017-12-01 08:26:02 +00:00
2017-11-29 00:51:41 +00:00
func (sh *SocketHandlers) GetSockets() map[string]Socket {
return sh.sockets
}
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) Validate() {
if sh.MaxMessageSize <= 0 {
sh.MaxMessageSize = DefaultMaxMessageSize
}
if sh.WriteTimeout <= 0 {
sh.WriteTimeout = DefaultWriteTimeout
}
if sh.ReadTimeout <= 0 {
sh.ReadTimeout = DefaultReadTimeout
}
}
func (sh *SocketHandlers) addSocket(soc Socket) {
sh.sockets[soc.ID()] = soc
}
func (sh *SocketHandlers) removeSocket(soc Socket) {
delete(sh.sockets, soc.ID())
}