diff --git a/conn_test.go b/conn_test.go index a3f3d787bd7755c21e99a01a8055c0a4e5242049..b9e2063d3cca29a7741092d36ee2e11a5ae753a6 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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")) diff --git a/netconn.go b/netconn.go index 4af6c202ae21ae1d37c3cd852e4f43b343048778..74000c9e8351d220508eb595fba06e884fc6e910 100644 --- a/netconn.go +++ b/netconn.go @@ -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. @@ -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) diff --git a/netconn_js.go b/netconn_js.go new file mode 100644 index 0000000000000000000000000000000000000000..ccc8c89fb2dca3a331d0ed6f1fae9639586cb123 --- /dev/null +++ b/netconn_js.go @@ -0,0 +1,11 @@ +package websocket + +import "net" + +func (nc *netConn) RemoteAddr() net.Addr { + return websocketAddr{} +} + +func (nc *netConn) LocalAddr() net.Addr { + return websocketAddr{} +} diff --git a/netconn_notjs.go b/netconn_notjs.go new file mode 100644 index 0000000000000000000000000000000000000000..f3eb0d66040d325a4422ecb64d313ca601452465 --- /dev/null +++ b/netconn_notjs.go @@ -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{} +}