add ClientAddr for websocket conn
This commit is contained in:
+16
-2
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user