Skip to content

Commit 8b5c88a

Browse files
committed
returns real remote and local address instead mocked
1 parent 8dee580 commit 8b5c88a

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

conn_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ func TestConn(t *testing.T) {
163163
n1.SetDeadline(time.Time{})
164164

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

169169
errs := xsync.Go(func() error {
170170
_, err := n2.Write([]byte("hello"))

netconn.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
// reading/writing goroutines are interrupted but the connection is kept alive.
3434
//
3535
// The Addr methods will return a mock net.Addr that returns "websocket" for Network
36-
// and "websocket/unknown-addr" for String.
36+
// and "websocket/unknown-addr" for String when RemoteAddr and LocalAddr methods do not exist.
3737
//
3838
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
3939
// io.EOF when reading.
@@ -133,14 +133,6 @@ func (a websocketAddr) String() string {
133133
return "websocket/unknown-addr"
134134
}
135135

136-
func (c *netConn) RemoteAddr() net.Addr {
137-
return websocketAddr{}
138-
}
139-
140-
func (c *netConn) LocalAddr() net.Addr {
141-
return websocketAddr{}
142-
}
143-
144136
func (c *netConn) SetDeadline(t time.Time) error {
145137
c.SetWriteDeadline(t)
146138
c.SetReadDeadline(t)

netconn_js.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package websocket
2+
3+
import "net"
4+
5+
func (c *netConn) RemoteAddr() net.Addr {
6+
return websocketAddr{}
7+
}
8+
9+
func (c *netConn) LocalAddr() net.Addr {
10+
return websocketAddr{}
11+
}

netconn_notjs.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// +build !js
2+
3+
package websocket
4+
5+
import "net"
6+
7+
type LocalRemoteAddr interface {
8+
RemoteAddr() net.Addr
9+
LocalAddr() net.Addr
10+
}
11+
12+
func (c *netConn) RemoteAddr() net.Addr {
13+
if ra, ok := c.c.rwc.(LocalRemoteAddr); ok {
14+
return ra.RemoteAddr()
15+
} else {
16+
return websocketAddr{}
17+
}
18+
}
19+
20+
func (c *netConn) LocalAddr() net.Addr {
21+
if la, ok := c.c.rwc.(LocalRemoteAddr); ok {
22+
return la.RemoteAddr()
23+
} else {
24+
return websocketAddr{}
25+
}
26+
}

0 commit comments

Comments
 (0)