mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-01-24 03:29:05 +08:00
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
// Package websocket
|
||
// @Link https://github.com/bufanyun/hotgo
|
||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||
// @Author Ms <133814250@qq.com>
|
||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||
//
|
||
package websocket
|
||
|
||
import (
|
||
"context"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/net/ghttp"
|
||
"github.com/gogf/gf/v2/os/gtime"
|
||
"github.com/gorilla/websocket"
|
||
"net/http"
|
||
)
|
||
|
||
var (
|
||
ctxManager context.Context // 主上下文
|
||
clientManager = NewClientManager() // 客户端管理
|
||
routers = make(map[string]EventHandler) // 消息路由
|
||
upGrader = websocket.Upgrader{
|
||
ReadBufferSize: 1024,
|
||
WriteBufferSize: 1024,
|
||
CheckOrigin: func(r *http.Request) bool {
|
||
return true
|
||
},
|
||
}
|
||
)
|
||
|
||
// Start 启动
|
||
func Start(c context.Context) {
|
||
ctxManager = c
|
||
g.Log().Info(ctxManager, "启动:WebSocket")
|
||
go clientManager.start()
|
||
go clientManager.ping()
|
||
}
|
||
|
||
// Stop 关闭
|
||
func Stop() {
|
||
clientManager.closeSignal <- struct{}{}
|
||
}
|
||
|
||
// WsPage ws入口
|
||
func WsPage(r *ghttp.Request) {
|
||
conn, err := upGrader.Upgrade(r.Response.ResponseWriter, r.Request, nil)
|
||
if err != nil {
|
||
return
|
||
}
|
||
currentTime := uint64(gtime.Now().Unix())
|
||
client := NewClient(r, conn, currentTime)
|
||
go client.read()
|
||
go client.write()
|
||
// 用户连接事件
|
||
clientManager.Register <- client
|
||
}
|