feat: server.Conn 支持在 WebSocket 模式下通过 GetWebsocketRequest 函数获取到请求
This commit is contained in:
parent
6e11c5edec
commit
42ab52bc66
|
@ -9,14 +9,18 @@ import (
|
||||||
"github.com/kercylan98/minotaur/utils/concurrent"
|
"github.com/kercylan98/minotaur/utils/concurrent"
|
||||||
"github.com/kercylan98/minotaur/utils/hash"
|
"github.com/kercylan98/minotaur/utils/hash"
|
||||||
"github.com/kercylan98/minotaur/utils/log"
|
"github.com/kercylan98/minotaur/utils/log"
|
||||||
|
"github.com/kercylan98/minotaur/utils/random"
|
||||||
"github.com/panjf2000/gnet"
|
"github.com/panjf2000/gnet"
|
||||||
"github.com/xtaci/kcp-go/v5"
|
"github.com/xtaci/kcp-go/v5"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var wsRequestKey = fmt.Sprintf("WS:REQ:%s", strings.ToUpper(random.HostName()))
|
||||||
|
|
||||||
// newKcpConn 创建一个处理KCP的连接
|
// newKcpConn 创建一个处理KCP的连接
|
||||||
func newKcpConn(server *Server, session *kcp.UDPSession) *Conn {
|
func newKcpConn(server *Server, session *kcp.UDPSession) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
|
@ -123,6 +127,11 @@ type connection struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetWebsocketRequest 获取websocket请求
|
||||||
|
func (slf *Conn) GetWebsocketRequest() *http.Request {
|
||||||
|
return slf.GetData(wsRequestKey).(*http.Request)
|
||||||
|
}
|
||||||
|
|
||||||
// IsEmpty 是否是空连接
|
// IsEmpty 是否是空连接
|
||||||
func (slf *Conn) IsEmpty() bool {
|
func (slf *Conn) IsEmpty() bool {
|
||||||
return slf.ws == nil && slf.gn == nil && slf.kcp == nil && slf.gw == nil
|
return slf.ws == nil && slf.gn == nil && slf.kcp == nil && slf.gw == nil
|
||||||
|
|
|
@ -299,6 +299,7 @@ func (slf *Server) Run(addr string) error {
|
||||||
}
|
}
|
||||||
ws.EnableWriteCompression(slf.websocketWriteCompression)
|
ws.EnableWriteCompression(slf.websocketWriteCompression)
|
||||||
conn := newWebsocketConn(slf, ws, ip)
|
conn := newWebsocketConn(slf, ws, ip)
|
||||||
|
conn.SetData(wsRequestKey, request)
|
||||||
for k, v := range request.URL.Query() {
|
for k, v := range request.URL.Query() {
|
||||||
if len(v) == 1 {
|
if len(v) == 1 {
|
||||||
conn.SetData(k, v[0])
|
conn.SetData(k, v[0])
|
||||||
|
|
Loading…
Reference in New Issue