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

Returns real remote and local address instead mocked #299

Merged
merged 1 commit into from
Oct 13, 2023
Merged
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
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{}
}
Loading