diff --git a/conn.go b/conn.go index d7434a9d..0f6abbf2 100644 --- a/conn.go +++ b/conn.go @@ -192,6 +192,22 @@ func (c *Conn) flate() bool { return c.copts != nil } +// RemoteAddr returns the remote address of websocket connection. +func (c *Conn) RemoteAddr() net.Addr { + if unc, ok := c.rwc.(net.Conn); ok { + return unc.RemoteAddr() + } + return websocketAddr{} +} + +// LocalAddr returns the local address of websocket connection. +func (c *Conn) LocalAddr() net.Addr { + if unc, ok := c.rwc.(net.Conn); ok { + return unc.LocalAddr() + } + return websocketAddr{} +} + // Ping sends a ping to the peer and waits for a pong. // Use this to measure latency or ensure the peer is responsive. // Ping must be called concurrently with Reader as it does diff --git a/internal/examples/echo/server.go b/internal/examples/echo/server.go index 37e2f2c4..3e22bd36 100644 --- a/internal/examples/echo/server.go +++ b/internal/examples/echo/server.go @@ -30,6 +30,8 @@ func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } defer c.CloseNow() + s.logf("new incoming connection: %s", c.RemoteAddr().String()) + if c.Subprotocol() != "echo" { c.Close(websocket.StatusPolicyViolation, "client must speak the echo subprotocol") return