Documentation ¶
Index ¶
- Constants
- func ReturnBeanError(err error) []byte
- func ReturnBeanFail(msg string) []byte
- func ReturnBeanMsg(msg string) []byte
- func ReturnBeanSuccess(data interface{}) []byte
- func SendMsg(host string, path string, query string, protocol []string, data []byte) error
- type Client
- func (c *Client) Close()
- func (c *Client) Heartbeat()
- func (c *Client) OnOpen(hub *Hub, w http.ResponseWriter, r *http.Request) (*Client, bool)
- func (c *Client) ReadPump(callbackOnMessage func(messageType int, receivedData []byte), ...)
- func (c *Client) SendMessage(messageType int, message []byte) error
- type ClientMoreParams
- type Hub
- type MsgBean
- type ReturnBean
Constants ¶
View Source
const ( WebsocketHandshakeSuccess = `{"code":200,"msg":"ws连接成功"}` WebsocketHandshakeError = `{"code":0,"msg":"发送消息格式不正确"}` WebsocketServerPingMsg = "Server->Ping->Client" WebsocketHeartbeatFailMaxTimes = 4 WebsocketWriteReadBufferSize = 20480 WebsocketMaxMessageSize = 65535 WebsocketPingPeriod = 20 WebsocketReadDeadline = 100 WebsocketWriteDeadline = 35 )
Variables ¶
This section is empty.
Functions ¶
func ReturnBeanError ¶
func ReturnBeanFail ¶
func ReturnBeanMsg ¶
func ReturnBeanSuccess ¶
func ReturnBeanSuccess(data interface{}) []byte
Types ¶
type Client ¶
type Client struct { Hub *Hub // 负责处理客户端注册、注销、在线管理 Conn *websocket.Conn // 一个ws连接 Send chan []byte // 一个ws连接存储自己的消息管道 PingPeriod time.Duration ReadDeadline time.Duration WriteDeadline time.Duration HeartbeatFailTimes int ClientLastPongTime time.Time // 客户端最近一次响应服务端 ping 消息的时间 State uint8 // ws状态,1=ok;0=出错、掉线等 sync.RWMutex ClientMoreParams // 这里追加一个结构体,方便开发者在成功上线后,可以自定义追加更多字段信息 }
func (*Client) Heartbeat ¶
func (c *Client) Heartbeat()
Heartbeat 按照websocket标准协议实现隐式心跳,Server端向Client远端发送ping格式数据包,浏览器收到ping标准格式,自动将消息原路返回给服务器
type ClientMoreParams ¶
type Hub ¶
type Hub struct { //上线注册 Register chan *Client //下线注销 UnRegister chan *Client //所有在线客户端的内存地址 Clients map[*Client]bool // contains filtered or unexported fields }
func CreateHubFactory ¶
func CreateHubFactory() *Hub
type MsgBean ¶
type MsgBean struct { Type string `json:"type,default=sendAll"` To string `json:"to,default=0"` Data string `json:"data,optional,omitempty"` }
MsgBean 发送
func GetMsgBean ¶
type ReturnBean ¶
type ReturnBean struct { Code uint32 `json:"code"` Msg string `json:"msg,optional,omitempty"` Data interface{} `json:"data,optional,omitempty"` }
ReturnBean 返回
func NewReturnBean ¶
func NewReturnBean(data interface{}, code uint32, msg string) *ReturnBean
NewReturnBean 创建ReturnBean
Click to show internal directories.
Click to hide internal directories.