Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:(conn): add RemoteAddr and LocalAddr method #490

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/examples/echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading