Skip to content

Latest commit

 

History

History
93 lines (80 loc) · 1.82 KB

File metadata and controls

93 lines (80 loc) · 1.82 KB

Websocket 封装

自己之前业务用到的

  1. Run 包方法, 启动服务
import github.com/github.com/yl10/kit/websocket
// 启动socket
go websocket.Run()
  1. Message 发送消息体格式
type Message struct {
	Code    int
	Message string
}
  1. Client 客户端
// NewClient 新建客户端
// user 是包含固定顺序值的客户端 slice
// user[0] 客户端ID
// user[1] 客户端SessionID
// user[2] 客户端描述或者名称
func NewClient(user []string, w http.ResponseWriter, r *http.Request) (*Client, error)
// Run 启动客户端并连接
func (c *Client) Run()
// WriteJson 手动发送 json 数据
func (c *Client) WriteJson(msg Message)
  1. Event 通讯事件
const (
	EVENT_HUB_REGISTE = iota
	EVENT_HUB_SENDMSG
	EVENT_HUB_UNREGISTE
)

const (
	EVENT_CLIENT_SENDMSG = iota
	EVENT_CLIENT_LEAVE
)

// NewEvent 定义一个新事件
func NewEvent(tp int, msg []byte, id string) Event
  1. Hub 通讯器
type Hub struct {
    // 客户端注册事件
	Register   chan *Client
    // 客户端断开事件
	Unregister chan Event
    // 广播事件
	Broadcast  chan Event
}

// NewHub 初始化一个通讯器
// 一般情况不需要用到
func NewHub() *Hub
  • Demo
  1. 客户端初始化
import github.com/github.com/yl10/kit/websocket

// 定义发送内容
// msg 为 []byte 格式 消息体
// clientId 为你需要发送给的对象ID
cli, err := websocket.NewClient(user, c.Ctx.ResponseWriter, c.Ctx.Request)
if err != nil {
    log.error(err)
    return
}
cli.Run()
  1. 向客户端发送数据
import github.com/github.com/yl10/kit/websocket

// 新建发送事件
send := websocket.NewEvent(websocket.EVENT_HUB_SENDMSG, msg, userId)
// 发送
websocket.GHub.Broadcast <- send