增加连接的connection_id

This commit is contained in:
2021-04-08 17:26:12 +08:00
parent fef46f3a0b
commit cf11a23a9d
5 changed files with 71 additions and 5 deletions

View File

@ -8,7 +8,12 @@
package context
import (
"fmt"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/go-developer/gopkg/util"
"gopkg.in/olahol/melody.v1"
)
@ -32,10 +37,22 @@ type WSContext struct {
// Date : 7:15 下午 2021/3/27
func NewContext(ginCtx *gin.Context, flag string, session *melody.Session) *WSContext {
return &WSContext{
ConnectionID: "",
ConnectionID: generateConnectionID(),
Flag: flag,
GinCtx: ginCtx,
Session: session,
Server: melody.New(),
}
}
// 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)))
}