Skip to content

Commit

Permalink
Merge pull request #299 from photostorm/net-addr
Browse files Browse the repository at this point in the history
Returns real remote and local address instead mocked
  • Loading branch information
nhooyr authored Oct 13, 2023
2 parents 9873274 + fecf26c commit 61942a4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
4 changes: 2 additions & 2 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func TestConn(t *testing.T) {
n1.SetDeadline(time.Time{})

assert.Equal(t, "remote addr", n1.RemoteAddr(), n1.LocalAddr())
assert.Equal(t, "remote addr string", "websocket/unknown-addr", n1.RemoteAddr().String())
assert.Equal(t, "remote addr network", "websocket", n1.RemoteAddr().Network())
assert.Equal(t, "remote addr string", "pipe", n1.RemoteAddr().String())
assert.Equal(t, "remote addr network", "pipe", n1.RemoteAddr().Network())

errs := xsync.Go(func() error {
_, err := n2.Write([]byte("hello"))
Expand Down
17 changes: 7 additions & 10 deletions netconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ import (
// where only the reading/writing goroutines are interrupted but the connection
// is kept alive.
//
// The Addr methods will return a mock net.Addr that returns "websocket" for Network
// and "websocket/unknown-addr" for String.
// The Addr methods will return the real addresses for connections obtained
// from websocket.Accept. But for connections obtained from websocket.Dial, a mock net.Addr
// will be returned that gives "websocket" for Network() and "websocket/unknown-addr" for
// String(). This is because websocket.Dial only exposes a io.ReadWriteCloser instead of the
// full net.Conn to us.
//
// When running as WASM, the Addr methods will always return the mock address described above.
//
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
// io.EOF when reading.
Expand Down Expand Up @@ -181,14 +186,6 @@ func (a websocketAddr) String() string {
return "websocket/unknown-addr"
}

func (nc *netConn) RemoteAddr() net.Addr {
return websocketAddr{}
}

func (nc *netConn) LocalAddr() net.Addr {
return websocketAddr{}
}

func (nc *netConn) SetDeadline(t time.Time) error {
nc.SetWriteDeadline(t)
nc.SetReadDeadline(t)
Expand Down
11 changes: 11 additions & 0 deletions netconn_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package websocket

import "net"

func (nc *netConn) RemoteAddr() net.Addr {
return websocketAddr{}
}

func (nc *netConn) LocalAddr() net.Addr {
return websocketAddr{}
}
20 changes: 20 additions & 0 deletions netconn_notjs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build !js
// +build !js

package websocket

import "net"

func (nc *netConn) RemoteAddr() net.Addr {
if unc, ok := nc.c.rwc.(net.Conn); ok {
return unc.RemoteAddr()
}
return websocketAddr{}
}

func (nc *netConn) LocalAddr() net.Addr {
if unc, ok := nc.c.rwc.(net.Conn); ok {
return unc.LocalAddr()
}
return websocketAddr{}
}

0 comments on commit 61942a4

Please sign in to comment.