Skip to content

Commit

Permalink
feat:(conn): add RemoteAddr method
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Sep 15, 2024
1 parent 6c8e3ab commit ed75b79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
client: false,
copts: copts,
flateThreshold: opts.CompressionThreshold,
remoteAddr: r.RemoteAddr,

br: brw.Reader,
bw: brw.Writer,
Expand Down
8 changes: 8 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Conn struct {
client bool
copts *compressionOptions
flateThreshold int
remoteAddr string
br *bufio.Reader
bw *bufio.Writer

Expand Down Expand Up @@ -88,6 +89,7 @@ type connConfig struct {
client bool
copts *compressionOptions
flateThreshold int
remoteAddr string

br *bufio.Reader
bw *bufio.Writer
Expand All @@ -100,6 +102,7 @@ func newConn(cfg connConfig) *Conn {
client: cfg.client,
copts: cfg.copts,
flateThreshold: cfg.flateThreshold,
remoteAddr: cfg.remoteAddr,

br: cfg.br,
bw: cfg.bw,
Expand Down Expand Up @@ -192,6 +195,11 @@ func (c *Conn) flate() bool {
return c.copts != nil
}

// RemoteAddr returns the remote address of websocket connection.
func (c *Conn) RemoteAddr() string {
return c.remoteAddr
}

// 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())

if c.Subprotocol() != "echo" {
c.Close(websocket.StatusPolicyViolation, "client must speak the echo subprotocol")
return
Expand Down

0 comments on commit ed75b79

Please sign in to comment.