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 1 commit
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
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 {
kehiy marked this conversation as resolved.
Show resolved Hide resolved
return c.remoteAddr
kehiy marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
Loading