升级上下文

This commit is contained in:
白茶清欢 2021-04-18 00:02:58 +08:00
parent 854c9b0ad7
commit 63be630c68

View File

@ -12,6 +12,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/go-developer/gopkg/easylock"
"github.com/go-developer/gopkg/easymap"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-developer/gopkg/util" "github.com/go-developer/gopkg/util"
"gopkg.in/olahol/melody.v1" "gopkg.in/olahol/melody.v1"
@ -28,6 +32,8 @@ type WSContext struct {
GinCtx *gin.Context // 基于gin实现websocket, gin的上下文 GinCtx *gin.Context // 基于gin实现websocket, gin的上下文
Session *melody.Session // 长连接的会话 Session *melody.Session // 长连接的会话
Server *melody.Melody // ws server Server *melody.Melody // ws server
Data easymap.EasyMap // 上下文中存储的数据
Lock easylock.EasyLock // 数据锁
} }
// NewContext 生成上下文信息 // NewContext 生成上下文信息
@ -36,12 +42,16 @@ type WSContext struct {
// //
// Date : 7:15 下午 2021/3/27 // Date : 7:15 下午 2021/3/27
func NewContext(ginCtx *gin.Context, flag string, session *melody.Session) *WSContext { func NewContext(ginCtx *gin.Context, flag string, session *melody.Session) *WSContext {
d, _ := easymap.NewSegment(128, true)
l, _ := easylock.NewSegment(128)
return &WSContext{ return &WSContext{
ConnectionID: generateConnectionID(), ConnectionID: generateConnectionID(),
Flag: flag, Flag: flag,
GinCtx: ginCtx, GinCtx: ginCtx,
Session: session, Session: session,
Server: melody.New(), Server: melody.New(),
Data: d,
Lock: l,
} }
} }