2021-04-18 21:55:41 +08:00
|
|
|
// Package websocket ...
|
|
|
|
//
|
|
|
|
// Description : 数据结构定义
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-18 21:55:41 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2021-11-17 18:30:49 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/context"
|
2021-04-18 21:55:41 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// buildResponseData 构建响应数据
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-18 21:55:41 +08:00
|
|
|
//
|
|
|
|
// 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<白茶清欢>
|
2021-04-18 21:55:41 +08:00
|
|
|
//
|
|
|
|
// 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()),
|
2021-04-18 21:55:41 +08:00
|
|
|
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<白茶清欢>
|
2021-04-18 21:55:41 +08:00
|
|
|
//
|
|
|
|
// 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<白茶清欢>
|
2021-04-18 21:55:41 +08:00
|
|
|
//
|
|
|
|
// 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<白茶清欢>
|
2021-04-18 21:55:41 +08:00
|
|
|
//
|
|
|
|
// 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:
|
2021-04-18 21:55:41 +08:00
|
|
|
loggerInstance.Info(message, fieldList...)
|
2021-11-17 18:39:59 +08:00
|
|
|
case logFuncWarn:
|
2021-04-18 21:55:41 +08:00
|
|
|
loggerInstance.Warn(message, fieldList...)
|
2021-11-17 18:39:59 +08:00
|
|
|
case logFuncError:
|
2021-04-18 21:55:41 +08:00
|
|
|
loggerInstance.Error(message, fieldList...)
|
2021-11-17 18:39:59 +08:00
|
|
|
case logFuncDPanic:
|
2021-04-18 21:55:41 +08:00
|
|
|
loggerInstance.DPanic(message, fieldList...)
|
2021-11-17 18:39:59 +08:00
|
|
|
case logFuncPanic:
|
2021-04-18 21:55:41 +08:00
|
|
|
loggerInstance.Panic(message, fieldList...)
|
2021-11-17 18:39:59 +08:00
|
|
|
case logFuncFatal:
|
2021-04-18 21:55:41 +08:00
|
|
|
loggerInstance.Fatal(message, fieldList...)
|
2021-11-17 18:39:59 +08:00
|
|
|
case logFuncDebug:
|
2021-04-18 21:55:41 +08:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
loggerInstance.Debug(message, fieldList...)
|
|
|
|
}
|
|
|
|
}
|