hotgo/server/internal/websocket/init.go
2022-11-24 23:37:34 +08:00

57 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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
}