feat: server.Conn 支持在 WebSocket 模式下通过 GetWebsocketRequest 函数获取到请求

This commit is contained in:
kercylan98 2023-10-16 19:08:25 +08:00
parent 6e11c5edec
commit 42ab52bc66
2 changed files with 10 additions and 0 deletions

View File

@ -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

View File

@ -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])