Documentation ¶
Index ¶
- Constants
- Variables
- func SetDebug(d bool)
- type Handler
- type HandshakeHead
- type KitConn
- type Message
- type MessageType
- type Packet
- type PacketDecoder
- type PacketType
- type RequestHead
- type RequestHeader
- type Route
- type Server
- type Session
- func (s *Session) Close(reason string)
- func (s *Session) HasKey(key string) bool
- func (s *Session) Push(route string, v interface{}) error
- func (s *Session) Response(req RequestHeader, v interface{}) error
- func (s *Session) Set(key string, value interface{})
- func (s *Session) String() string
- func (s *Session) Value(key string) interface{}
- func (s *Session) Write(t MessageType, msgId uint, route string, data interface{}) error
- type SessionCloseEventListener
- type SessionManager
Constants ¶
const ( KitConnStatusCreated = iota KitConnStatusHandshake KitConnStatusWorking KitConnStatusClosed )
const ( MessageRequest MessageType = 0x00 MessageNotify = 0x01 MessageResponse = 0x02 MessagePush = 0x03 )
Message types
const ( PacketHeadLength = 4 PacketMaxSize = 64 * 1024 )
Codec constants.
const ( // Handshake represents a handshake: request(client) <====> handshake response(server) PacketHandshake = 0x01 // HandshakeAck represents a handshake ack from client to server PacketHandshakeAck = 0x02 // Heartbeat represents a heartbeat PacketHeartbeat = 0x03 // Data represents a common data packet PacketData = 0x04 // Kick represents a kick off packet PacketClose = 0x05 // disconnect message from server )
const ( SessionStatusNormal = iota SessionStatusClosed )
const (
KitConnWriteQueueSize = 128
)
Variables ¶
var ( ErrInvalidConnStatus = errors.New("write conn while not working status") ErrBufferExceed = errors.New("session send buffer exceed") HeartbeatPacket []byte ConnClosePacket []byte )
var ( ErrWrongMessageType = errors.New("wrong message type") ErrInvalidMessage = errors.New("invalid message") )
Errors that could be occurred in message codec
var ErrPacketSizeExcced = errors.New("kit:packet size exceed")
ErrPacketSizeExcced is the error used for encode/decode.
var ErrWrongPacketType = errors.New("kit:wrong packet type")
ErrWrongPacketType represents a wrong packet type.
var Logger *zap.SugaredLogger = nil
var SessionMaxDelayMsgCount = 100 // 必须比 KitConnWriteQueueSize 小
Functions ¶
Types ¶
type HandshakeHead ¶
type HandshakeHead struct {
SessionId string `json:"sid"`
}
type KitConn ¶
type Message ¶
type Message struct { Type MessageType // message type ID uint // unique id, zero while notify mode Route string // route for locating service Data []byte // payload }
Message represents a unmarshaled message or a message which to be marshaled
func DecodeMessageFromRaw ¶
Decode unmarshal the bytes slice to a message See ref: https://github.com/lonnng/nano/blob/master/docs/communication_protocol.md
func NewMessage ¶
func NewMessage(t MessageType, id uint, route string, data interface{}) *Message
func (*Message) Encode ¶
Encode marshals message to binary format. Different message types is corresponding to different message header, message types is identified by 2-4 bit of flag field. The relationship between message types and message header is presented as follows: ------------------------------------------ | type | flag | other | |----------|--------|--------------------| | request |----000-|<message id>|<route>| | notify |----001-|<route> | | response |----010-|<message id> | | push |----011-|<route> | ------------------------------------------ The figure above indicates that the bit does not affect the type of message. See ref: https://github.com/lonnng/nano/blob/master/docs/communication_protocol.md
type MessageType ¶
type MessageType byte
Type represents the type of message, which could be Request/Notify/Response/Push
type Packet ¶
type Packet struct { Type PacketType Data []byte }
Packet represents a network packet.
func (*Packet) Encode ¶
Encode create a packet.Packet from the raw bytes slice and then encode to network bytes slice Protocol refs: https://github.com/NetEase/pomelo/wiki/Communication-Protocol
-<type>-|--------<length>--------|-<data>- --------|------------------------|-------- 1 byte packet type, 3 bytes packet data length(big end), and data segment
type PacketDecoder ¶
type PacketDecoder struct {
// contains filtered or unexported fields
}
A PacketDecoder reads and decodes network data slice
func NewPacketDecoder ¶
func NewPacketDecoder() *PacketDecoder
NewPacketDecoder returns a new decoder that used for decode network bytes slice.
type PacketType ¶
type PacketType byte
type RequestHead ¶
type RequestHead struct {
MsgId uint `json:"-"`
}
func (*RequestHead) GetMsgId ¶
func (req *RequestHead) GetMsgId() uint
func (*RequestHead) SetMsgId ¶
func (req *RequestHead) SetMsgId(id uint)
type RequestHeader ¶
type Server ¶
type Server struct { HeartbeatInterval time.Duration SessionManager *SessionManager Route *Route }
func (*Server) RunWebSocketServer ¶
type Session ¶
type Session struct { sync.RWMutex Manager *SessionManager Id string LostConnection time.Time // contains filtered or unexported fields }
func (*Session) Response ¶
func (s *Session) Response(req RequestHeader, v interface{}) error
type SessionCloseEventListener ¶
type SessionCloseEventListener interface {
OnSessionClose(s *Session)
}
type SessionManager ¶
func NewSessionManager ¶
func NewSessionManager() *SessionManager
func (*SessionManager) CheckExpire ¶
func (m *SessionManager) CheckExpire()
func (*SessionManager) GetSessionById ¶
func (m *SessionManager) GetSessionById(id string) *Session