121 lines
2.7 KiB
Go
121 lines
2.7 KiB
Go
// Package instance ...
|
|
//
|
|
// Description : instance ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2025-04-25 18:16
|
|
package instance
|
|
|
|
import (
|
|
"git.zhangdeman.cn/zhangdeman/websocket"
|
|
"git.zhangdeman.cn/zhangdeman/websocket/abstract"
|
|
"git.zhangdeman.cn/zhangdeman/websocket/config"
|
|
"git.zhangdeman.cn/zhangdeman/websocket/context"
|
|
"git.zhangdeman.cn/zhangdeman/websocket/storage"
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
)
|
|
|
|
// NewWebsocketLog 初始化长连接日志
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:12 2024/7/22
|
|
func NewWebsocketLog(serverPort int, route *gin.Engine) (*WsLog, error) {
|
|
wsLog := &WsLog{}
|
|
if err := websocket.NewWebsocketServeWithGinRouter(serverPort, route, wsLog); nil != err {
|
|
return nil, err
|
|
}
|
|
wsServer, _ := websocket.GetWsServer(serverPort, wsLog.GetModuleFlag())
|
|
// logger.SetWsLoggerConnect(wsServer.GetConfig().ConnectionManager)
|
|
return &WsLog{
|
|
port: serverPort,
|
|
connectManager: wsServer.GetConfig().ConnectionManager,
|
|
}, nil
|
|
}
|
|
|
|
// CloseWebsocketLog ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 13:27 2024/7/25
|
|
func CloseWebsocketLog(wsLog *WsLog) {
|
|
if nil == wsLog {
|
|
return
|
|
}
|
|
wsServer, _ := websocket.GetWsServer(wsLog.port, wsLog.GetModuleFlag())
|
|
allConnect := wsServer.GetConfig().ConnectionManager.GetCtxList()
|
|
for _, conn := range allConnect {
|
|
_ = conn.Session.Close()
|
|
}
|
|
}
|
|
|
|
type WsLog struct {
|
|
port int
|
|
connectManager storage.IConnection
|
|
}
|
|
|
|
func (e WsLog) Connect(ctx *context.WSContext) error {
|
|
return nil
|
|
}
|
|
|
|
func (e WsLog) Disconnect(ctx *context.WSContext) {
|
|
}
|
|
|
|
func (e WsLog) Close(ctx *context.WSContext, code int, message string) error {
|
|
return nil
|
|
}
|
|
|
|
func (e WsLog) HandshakeURL() []string {
|
|
return []string{
|
|
"/system/logger/trace",
|
|
}
|
|
}
|
|
|
|
func (e WsLog) GetCommandList() []abstract.ICommand {
|
|
return []abstract.ICommand{}
|
|
}
|
|
|
|
func (e WsLog) GetModuleFlag() string {
|
|
return "ws-log"
|
|
}
|
|
|
|
func (e WsLog) GetServerPort() int {
|
|
return e.port
|
|
}
|
|
|
|
func (e WsLog) GetWSServerConfig() []config.SetWSServerConfig {
|
|
return []config.SetWSServerConfig{
|
|
config.SetPingPeriod(5),
|
|
config.SetPongWait(60),
|
|
config.SetWriteWait(60),
|
|
config.SetMaxMessageBufferSize(81920),
|
|
config.SetMaxMessageSize(81920),
|
|
}
|
|
}
|
|
|
|
func (e WsLog) Writer() io.Writer {
|
|
return &wsWriter{
|
|
connectionManager: e.connectManager,
|
|
}
|
|
}
|
|
|
|
type wsWriter struct {
|
|
connectionManager storage.IConnection
|
|
}
|
|
|
|
// Write ws的writer
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:24 2024/7/22
|
|
func (w *wsWriter) Write(p []byte) (n int, err error) {
|
|
p = []byte(" " + string(p))
|
|
allConnList := w.connectionManager.GetCtxList()
|
|
for _, conn := range allConnList {
|
|
_ = conn.Session.Write(p)
|
|
}
|
|
return 0, nil
|
|
}
|