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"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-06-27 15:50:41 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/easylock"
|
2021-04-18 00:02:58 +08:00
|
|
|
|
2022-06-27 15:50:41 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/easymap"
|
2021-04-18 00:02:58 +08:00
|
|
|
|
2022-06-27 15:50:41 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/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 {
|
2021-04-18 20:11:48 +08:00
|
|
|
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
|
2021-03-28 16:04:42 +08:00
|
|
|
func NewContext(ginCtx *gin.Context, flag string, session *melody.Session) *WSContext {
|
2021-04-18 00:02:58 +08:00
|
|
|
d, _ := easymap.NewSegment(128, true)
|
|
|
|
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),
|
2021-04-18 20:11:48 +08:00
|
|
|
TraceID: generateConnectionID(flag),
|
2021-03-28 16:04:42 +08:00
|
|
|
Flag: flag,
|
2021-03-27 19:24:08 +08:00
|
|
|
GinCtx: ginCtx,
|
|
|
|
Session: session,
|
2021-03-28 16:04:42 +08:00
|
|
|
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
|
|
|
|
2021-04-18 20:11:48 +08:00
|
|
|
//CloneContext 克隆上下文信息
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-18 20:11:48 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2022-06-27 15:50:41 +08:00
|
|
|
strings.ReplaceAll(util.IP.GetHostIP(), ".", ""),
|
2021-04-08 17:26:12 +08:00
|
|
|
time.Now().Format("20060102150405"),
|
2022-06-27 15:50:41 +08:00
|
|
|
util.String.Md5(fmt.Sprintf("%d", time.Now().UnixNano())+util.String.GenRandom("", 64)))
|
2021-04-08 17:26:12 +08:00
|
|
|
}
|