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(id string, wsConn *websocket.Conn) error
// 从集合中获取对应id的连接
Get(id string) (wsConn *websocket.Conn, err error)
// 通过groupName获取对应的连接列表
GetGroup(groupName string) (wsConnList []*websocket.Conn, err error)
// 获取所有连接
GetAll() (wsConnList []*websocket.Conn, err error)
// 将id对应的连接从集合中删除
Del(id string) error
}
Collection描述了客户端维护的websocket在线连接接口的具体类型.
type HeartBeater ¶ added in v0.0.5
type HeartBeater interface {
// 校验是否客户端Ping请求
IsPingMsg(msg []byte) bool
// 获取服务端->客户端Pong请求数据
GetPongMsg() []byte
// 获取心跳间隔有效时间(单位是秒)。如果两次心跳大于这个有效时间,连接将断开
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连接. collect为Collection接口类的具体实现;heartBeater为HeartBeater接口的具体实现.
func (*WsConnection) Close ¶
func (conn *WsConnection) Close() error
func (*WsConnection) GetWsConnId ¶ added in v0.0.5
func (conn *WsConnection) GetWsConnId() string
GetWsConnId获取WsConnection中唯一连接id
func (*WsConnection) Open ¶
func (conn *WsConnection) Open(w http.ResponseWriter, r *http.Request) error
func (*WsConnection) Receive ¶
func (conn *WsConnection) Receive() (msg *WsMessage, err error)
func (*WsConnection) Write ¶
func (conn *WsConnection) Write(msg *WsMessage) (err error)
Click to show internal directories.
Click to hide internal directories.