Files
iotlcm/server/internal/library/websocket/init.go
T
MiFaZhan 59eb34976a chore: 初始开源发布
iotlcm-admin-svr:管理后台服务。

- Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件)
- 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、
  MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位;
  私网 IP → 文档地址段;库名 xiujie_admin → iotlcm
- 真实手机号/邮箱 → 合法 11 位编造号 / example.com
- 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、
  私有依赖 go get 行、明文 registry 登录口令
- 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名
- Dockerfile 改用公共基础镜像
- 拼错的 RADEME.md → README.md,README 重写
- 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用
- 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes

数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
2026-10-23 14:48:30 +08:00

64 lines
1.7 KiB
Go

// Package websocket
// @Link https://github.com/xiujiecn/xiu-admin
// @Copyright Copyright (c) 2025 LiXiujie
// @Author Lxj <li@xiujie.cn>
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
package websocket
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
"net/http"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gorilla/websocket"
)
var (
mctx = gctx.GetInitCtx() // 上下文
clientManager = NewClientManager() // 管理者
)
var upGrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func StartWebSocket(ctx context.Context) {
g.Log().Info(ctx, "启动:WebSocket")
go clientManager.start()
go clientManager.ping(ctx)
}
func StopWebSocket(ctx context.Context) {
g.Log().Info(ctx, "停止:WebSocket")
clientManager.closeSignal <- struct{}{}
}
func WsPage(r *ghttp.Request) {
g.Log().Infof(r.GetCtx(), "websocket.WsPage Begin. UserID:%v ", contexts.GetUserId(r.GetCtx()))
// r.Response.Header().Set("Content-Type", "text/event-stream")
contexts.SetIsWebSocket(r.GetCtx(), true)
conn, err := upGrader.Upgrade(r.Response.Writer, r.Request, nil)
if err != nil {
r.Response.Write(err.Error())
return
}
currentTime := uint64(gtime.Now().Unix())
client := NewClient(conn.RemoteAddr().String(), conn, currentTime, r.GetCtx())
client.UserId = uint64(contexts.GetUserId(r.GetCtx()))
go client.read()
go client.write()
// 用户连接事件
clientManager.Register <- client
g.Log().Infof(r.GetCtx(), "websocket.WsPage End. UserID:%v", client.UserId)
}