websocket/context/context.go

95 lines
2.5 KiB
Go
Raw Normal View History

2021-03-27 19:24:08 +08:00
// Package context...
//
// Description : 上下文信息
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-03-27 19:24:08 +08:00
//
// Date : 2021-03-27 6:53 下午
package context
import (
2021-04-08 17:26:12 +08:00
"fmt"
2024-07-22 16:40:33 +08:00
"git.zhangdeman.cn/zhangdeman/wrapper"
2021-04-08 17:26:12 +08:00
"strings"
"time"
"git.zhangdeman.cn/zhangdeman/easylock"
2021-04-18 00:02:58 +08:00
"git.zhangdeman.cn/zhangdeman/easymap"
2021-04-18 00:02:58 +08:00
2024-07-22 16:40:33 +08:00
"git.zhangdeman.cn/zhangdeman/network/util"
2021-03-27 19:24:08 +08:00
"github.com/gin-gonic/gin"
"gopkg.in/olahol/melody.v1"
)
// WSContext 请求上下文
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-03-27 19:24:08 +08:00
//
// Date : 7:03 下午 2021/3/27
type WSContext struct {
ConnectionID string // 请求ID / 连接ID, 一个生命周期内, 此值不会发生变化
TraceID string // 请求ID,每次指令执行都会发生变化,是针对一次请求有效的
2021-04-18 00:02:58 +08:00
Flag string // 长连接模块
GinCtx *gin.Context // 基于gin实现websocket, gin的上下文
Session *melody.Session // 长连接的会话
Server *melody.Melody // ws server
Data easymap.EasyMap // 上下文中存储的数据
Lock easylock.EasyLock // 数据锁
2021-03-27 19:24:08 +08:00
}
// NewContext 生成上下文信息
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-03-27 19:24:08 +08:00
//
// Date : 7:15 下午 2021/3/27
func NewContext(ginCtx *gin.Context, flag string, session *melody.Session) *WSContext {
2024-07-22 16:40:33 +08:00
d, _ := easymap.NewSegment(128)
2021-04-18 00:02:58 +08:00
l, _ := easylock.NewSegment(128)
2021-03-27 19:24:08 +08:00
return &WSContext{
2021-04-18 01:02:17 +08:00
ConnectionID: generateConnectionID(flag),
TraceID: generateConnectionID(flag),
Flag: flag,
2021-03-27 19:24:08 +08:00
GinCtx: ginCtx,
Session: session,
Server: melody.New(),
2021-04-18 00:02:58 +08:00
Data: d,
Lock: l,
2021-03-27 19:24:08 +08:00
}
}
2021-04-08 17:26:12 +08:00
2024-07-22 16:40:33 +08:00
// CloneContext 克隆上下文信息
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:07 下午 2021/4/18
func CloneContext(ctx *WSContext) *WSContext {
if nil == ctx {
return nil
}
return &WSContext{
ConnectionID: ctx.ConnectionID,
TraceID: generateConnectionID(ctx.Flag),
Flag: ctx.Flag,
GinCtx: ctx.GinCtx,
Session: ctx.Session,
Server: ctx.Server,
Data: ctx.Data,
Lock: ctx.Lock,
}
}
2021-04-08 17:26:12 +08:00
// generateConnectionID 生成connection_id
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-04-08 17:26:12 +08:00
//
// Date : 5:18 下午 2021/4/8
2021-04-18 01:02:17 +08:00
func generateConnectionID(flag string) string {
return fmt.Sprintf("%s-%s-%s-%s",
flag,
strings.ReplaceAll(util.IP.GetHostIP(), ".", ""),
2021-04-08 17:26:12 +08:00
time.Now().Format("20060102150405"),
2024-07-23 15:52:10 +08:00
wrapper.String(fmt.Sprintf("%d", time.Now().UnixNano())+wrapper.StringFromRandom(64, "").Value()).Md5().Value,
2024-07-22 16:40:33 +08:00
)
2021-04-08 17:26:12 +08:00
}