add ClientAddr for websocket conn

This commit is contained in:
ginuerzh
2024-10-16 20:46:45 +08:00
parent ea179a0ee6
commit 24547b4332
14 changed files with 98 additions and 37 deletions
+16 -2
View File
@@ -5,6 +5,7 @@ import (
"sync"
"time"
xnet "github.com/go-gost/x/internal/net"
"github.com/gorilla/websocket"
)
@@ -12,12 +13,14 @@ type WebsocketConn interface {
net.Conn
WriteMessage(int, []byte) error
ReadMessage() (int, []byte, error)
xnet.ClientAddr
}
type websocketConn struct {
*websocket.Conn
rb []byte
mux sync.Mutex
rb []byte
clientAddr net.Addr
mux sync.Mutex
}
func Conn(conn *websocket.Conn) WebsocketConn {
@@ -26,6 +29,13 @@ func Conn(conn *websocket.Conn) WebsocketConn {
}
}
func ConnWithClientAddr(conn *websocket.Conn, clientAddr net.Addr) WebsocketConn {
return &websocketConn{
Conn: conn,
clientAddr: clientAddr,
}
}
func (c *websocketConn) Read(b []byte) (n int, err error) {
if len(c.rb) == 0 {
_, c.rb, err = c.Conn.ReadMessage()
@@ -66,3 +76,7 @@ func (c *websocketConn) SetWriteDeadline(t time.Time) error {
defer c.mux.Unlock()
return c.Conn.SetWriteDeadline(t)
}
func (c *websocketConn) ClientAddr() net.Addr {
return c.clientAddr
}