Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // websocket连接已关闭 ErrWsConnClose = errors.New("websocket connection closed") // websocket唯一连接id不能为空 ErrIdEmpty = errors.New("id is null") )
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection interface {
// Set 将连接写入集合
Set(id string, wsConn *WsConnection) error
// Get 从集合中获取对应id的连接
Get(id string) (wsConn *WsConnection, err error)
// GetGroup 通过groupId获取对应的连接列表
GetGroup(groupId string) (wsConnList []*WsConnection, err error)
// GetAll 获取所有连接
GetAll() (wsConnList []*WsConnection, err error)
// Del 将id对应的连接从集合中删除
Del(id string) error
}
Collection 业务方实现维护长连接的接口.
type HeartBeater ¶ added in v0.0.5
type HeartBeater interface {
// IsPingMsg 校验是否客户端Ping请求
IsPingMsg(msg []byte) bool
// GetPongMsg 获取服务端->客户端Pong请求数据
GetPongMsg() []byte
// GetHeartbeatTime 获取心跳间隔有效时间(单位是秒)。如果两次心跳大于这个有效时间,连接将断开
GetHeartbeatTime() int
}
HeartBeater 业务方实现维持心跳的接口
type WsConnection ¶
type WsConnection struct {
// contains filtered or unexported fields
}
WsConnection 维护的websocket长连接.
func NewWsConnection ¶
func NewWsConnection(collect Collection, heartBeater HeartBeater) *WsConnection
NewWsConnection 新建一个WsConnection类型的长连接.
func (*WsConnection) Close ¶
func (wc *WsConnection) Close() error
func (*WsConnection) GetWsConnId ¶ added in v0.0.5
func (wc *WsConnection) GetWsConnId() string
GetWsConnId 获取WsConnection中唯一连接id
func (*WsConnection) Open ¶
func (wc *WsConnection) Open(w http.ResponseWriter, r *http.Request) error
func (*WsConnection) Receive ¶
func (wc *WsConnection) Receive() (msg *WsMessage, err error)
func (*WsConnection) Write ¶
func (wc *WsConnection) Write(msg *WsMessage) (err error)
Click to show internal directories.
Click to hide internal directories.