2021-03-27 19:24:08 +08:00
|
|
|
// Package context...
|
|
|
|
//
|
|
|
|
// Description : 上下文信息
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 2021-03-27 6:53 下午
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2021-04-08 17:26:12 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-03-27 19:24:08 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-04-08 17:26:12 +08:00
|
|
|
"github.com/go-developer/gopkg/util"
|
2021-03-27 19:24:08 +08:00
|
|
|
"gopkg.in/olahol/melody.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WSContext 请求上下文
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 7:03 下午 2021/3/27
|
|
|
|
type WSContext struct {
|
|
|
|
ConnectionID string // 请求ID
|
2021-03-28 16:04:42 +08:00
|
|
|
Flag string // 长连接模块
|
2021-03-27 19:24:08 +08:00
|
|
|
GinCtx *gin.Context // 基于gin实现websocket, gin的上下文
|
|
|
|
Session *melody.Session // 长连接的会话
|
2021-03-28 16:04:42 +08:00
|
|
|
Server *melody.Melody // ws server
|
2021-03-27 19:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext 生成上下文信息
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// 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-03-27 19:24:08 +08:00
|
|
|
return &WSContext{
|
2021-04-08 17:26:12 +08:00
|
|
|
ConnectionID: generateConnectionID(),
|
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-03-27 19:24:08 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 17:26:12 +08:00
|
|
|
|
|
|
|
// generateConnectionID 生成connection_id
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 5:18 下午 2021/4/8
|
|
|
|
func generateConnectionID() string {
|
|
|
|
return fmt.Sprintf("%s-%s-%s",
|
|
|
|
strings.ReplaceAll(util.GetHostIP(), ".", ""),
|
|
|
|
time.Now().Format("20060102150405"),
|
|
|
|
util.Md5(fmt.Sprintf("%d", time.Now().UnixNano())+util.GenRandomString("", 64)))
|
|
|
|
}
|