feat: server.Conn 支持获取连接打开时间及在线时长
This commit is contained in:
parent
a4bc8280a4
commit
18a0b06e0e
|
@ -17,6 +17,7 @@ import (
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var wsRequestKey = fmt.Sprintf("WS:REQ:%s", strings.ToUpper(random.HostName()))
|
var wsRequestKey = fmt.Sprintf("WS:REQ:%s", strings.ToUpper(random.HostName()))
|
||||||
|
@ -31,6 +32,7 @@ func newKcpConn(server *Server, session *kcp.UDPSession) *Conn {
|
||||||
ip: session.RemoteAddr().String(),
|
ip: session.RemoteAddr().String(),
|
||||||
kcp: session,
|
kcp: session,
|
||||||
data: map[any]any{},
|
data: map[any]any{},
|
||||||
|
openTime: time.Now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if index := strings.LastIndex(c.ip, ":"); index != -1 {
|
if index := strings.LastIndex(c.ip, ":"); index != -1 {
|
||||||
|
@ -50,6 +52,7 @@ func newGNetConn(server *Server, conn gnet.Conn) *Conn {
|
||||||
ip: conn.RemoteAddr().String(),
|
ip: conn.RemoteAddr().String(),
|
||||||
gn: conn,
|
gn: conn,
|
||||||
data: map[any]any{},
|
data: map[any]any{},
|
||||||
|
openTime: time.Now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if index := strings.LastIndex(c.ip, ":"); index != -1 {
|
if index := strings.LastIndex(c.ip, ":"); index != -1 {
|
||||||
|
@ -69,27 +72,13 @@ func newWebsocketConn(server *Server, ws *websocket.Conn, ip string) *Conn {
|
||||||
ip: ip,
|
ip: ip,
|
||||||
ws: ws,
|
ws: ws,
|
||||||
data: map[any]any{},
|
data: map[any]any{},
|
||||||
|
openTime: time.Now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
c.writeLoop()
|
c.writeLoop()
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// newGatewayConn 创建一个处理网关消息的连接
|
|
||||||
func newGatewayConn(conn *Conn, connId string) *Conn {
|
|
||||||
c := &Conn{
|
|
||||||
//ctx: server.ctx,
|
|
||||||
connection: &connection{
|
|
||||||
server: conn.server,
|
|
||||||
data: map[any]any{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
c.gw = func(packet []byte) {
|
|
||||||
conn.Write(packet)
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewEmptyConn 创建一个适用于测试的空连接
|
// NewEmptyConn 创建一个适用于测试的空连接
|
||||||
func NewEmptyConn(server *Server) *Conn {
|
func NewEmptyConn(server *Server) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
|
@ -99,6 +88,7 @@ func NewEmptyConn(server *Server) *Conn {
|
||||||
remoteAddr: &net.TCPAddr{},
|
remoteAddr: &net.TCPAddr{},
|
||||||
ip: "0.0.0.0:0",
|
ip: "0.0.0.0:0",
|
||||||
data: map[any]any{},
|
data: map[any]any{},
|
||||||
|
openTime: time.Now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
c.writeLoop()
|
c.writeLoop()
|
||||||
|
@ -125,6 +115,7 @@ type connection struct {
|
||||||
pool *concurrent.Pool[*connPacket]
|
pool *concurrent.Pool[*connPacket]
|
||||||
loop *writeloop.WriteLoop[*connPacket]
|
loop *writeloop.WriteLoop[*connPacket]
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
openTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetServer 获取服务器
|
// GetServer 获取服务器
|
||||||
|
@ -132,6 +123,16 @@ func (slf *Conn) GetServer() *Server {
|
||||||
return slf.server
|
return slf.server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetOpenTime 获取连接打开时间
|
||||||
|
func (slf *Conn) GetOpenTime() time.Time {
|
||||||
|
return slf.openTime
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOnlineTime 获取连接在线时长
|
||||||
|
func (slf *Conn) GetOnlineTime() time.Duration {
|
||||||
|
return time.Now().Sub(slf.openTime)
|
||||||
|
}
|
||||||
|
|
||||||
// GetWebsocketRequest 获取websocket请求
|
// GetWebsocketRequest 获取websocket请求
|
||||||
func (slf *Conn) GetWebsocketRequest() *http.Request {
|
func (slf *Conn) GetWebsocketRequest() *http.Request {
|
||||||
return slf.GetData(wsRequestKey).(*http.Request)
|
return slf.GetData(wsRequestKey).(*http.Request)
|
||||||
|
|
Loading…
Reference in New Issue