From 42ab52bc668d28194a56f2c9de09833381b36d8f Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 16 Oct 2023 19:08:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20server.Conn=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=9C=A8=20WebSocket=20=E6=A8=A1=E5=BC=8F=E4=B8=8B=E9=80=9A?= =?UTF-8?q?=E8=BF=87=20GetWebsocketRequest=20=E5=87=BD=E6=95=B0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=88=B0=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/conn.go | 9 +++++++++ server/server.go | 1 + 2 files changed, 10 insertions(+) diff --git a/server/conn.go b/server/conn.go index 62ba494..8a2c7e8 100644 --- a/server/conn.go +++ b/server/conn.go @@ -9,14 +9,18 @@ import ( "github.com/kercylan98/minotaur/utils/concurrent" "github.com/kercylan98/minotaur/utils/hash" "github.com/kercylan98/minotaur/utils/log" + "github.com/kercylan98/minotaur/utils/random" "github.com/panjf2000/gnet" "github.com/xtaci/kcp-go/v5" "net" + "net/http" "runtime/debug" "strings" "sync" ) +var wsRequestKey = fmt.Sprintf("WS:REQ:%s", strings.ToUpper(random.HostName())) + // newKcpConn 创建一个处理KCP的连接 func newKcpConn(server *Server, session *kcp.UDPSession) *Conn { c := &Conn{ @@ -123,6 +127,11 @@ type connection struct { mu sync.Mutex } +// GetWebsocketRequest 获取websocket请求 +func (slf *Conn) GetWebsocketRequest() *http.Request { + return slf.GetData(wsRequestKey).(*http.Request) +} + // IsEmpty 是否是空连接 func (slf *Conn) IsEmpty() bool { return slf.ws == nil && slf.gn == nil && slf.kcp == nil && slf.gw == nil diff --git a/server/server.go b/server/server.go index 1e882eb..1ce45cf 100644 --- a/server/server.go +++ b/server/server.go @@ -299,6 +299,7 @@ func (slf *Server) Run(addr string) error { } ws.EnableWriteCompression(slf.websocketWriteCompression) conn := newWebsocketConn(slf, ws, ip) + conn.SetData(wsRequestKey, request) for k, v := range request.URL.Query() { if len(v) == 1 { conn.SetData(k, v[0])