websocket/define.go

114 lines
2.7 KiB
Go
Raw Normal View History

// Package websocket ...
//
// Description : 数据结构定义
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-04-18 7:53 下午
package websocket
import (
"encoding/json"
"time"
2023-06-07 16:08:16 +08:00
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/websocket/context"
"go.uber.org/zap"
)
// buildResponseData 构建响应数据
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:23 下午 2021/4/18
func buildResponseData(wsCtx *context.WSContext, cmd string, data interface{}) []byte {
r := map[string]interface{}{
"connection_id": wsCtx.ConnectionID,
"trace_id": wsCtx.TraceID,
"command": cmd,
"data": data,
}
byteData, _ := json.Marshal(r)
return byteData
}
// getLoadDataList 获取需记录的数据列表
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:00 下午 2021/4/18
func getLoadDataList(wsContext *context.WSContext, fieldList ...zap.Field) []zap.Field {
if nil == wsContext {
wsContext = &context.WSContext{}
}
list := []zap.Field{
zap.String("connection_id", wsContext.ConnectionID),
zap.String("trace_id", wsContext.TraceID),
zap.String("module_flag", wsContext.Flag),
2023-06-07 16:08:16 +08:00
zap.String("server_ip", util.IP.GetHostIP()),
zap.Int64("timestamp", time.Now().UnixNano()),
}
list = append(list, fieldList...)
return list
}
const (
logFuncDebug = "debug"
logFuncInfo = "info"
logFuncWarn = "warn"
logFuncError = "error"
logFuncDPanic = "dpanic"
logFuncPanic = "panic"
logFuncFatal = "fatal"
)
// getWsServer 获取WS-Server实例
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:54 下午 2021/4/18
func getWsServer(port int, flag string) *Server {
return wsServerTable[port][flag]
}
// getLoggerInstance 获取日志实例
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:48 下午 2021/4/18
func getLoggerInstance(moduleFlag string, cmdAllow *bool) *zap.Logger {
if nil != cmdAllow && !*cmdAllow {
return nil
}
return loggerInstanceTable[moduleFlag]
}
// log 记录日志
//
2021-06-05 22:13:09 +08:00
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:29 下午 2021/4/18
func log(loggerInstance *zap.Logger, f string, message string, fieldList []zap.Field) {
if nil == loggerInstance {
return
}
switch f {
2021-11-17 18:39:59 +08:00
case logFuncInfo:
loggerInstance.Info(message, fieldList...)
2021-11-17 18:39:59 +08:00
case logFuncWarn:
loggerInstance.Warn(message, fieldList...)
2021-11-17 18:39:59 +08:00
case logFuncError:
loggerInstance.Error(message, fieldList...)
2021-11-17 18:39:59 +08:00
case logFuncDPanic:
loggerInstance.DPanic(message, fieldList...)
2021-11-17 18:39:59 +08:00
case logFuncPanic:
loggerInstance.Panic(message, fieldList...)
2021-11-17 18:39:59 +08:00
case logFuncFatal:
loggerInstance.Fatal(message, fieldList...)
2021-11-17 18:39:59 +08:00
case logFuncDebug:
fallthrough
default:
loggerInstance.Debug(message, fieldList...)
}
}