Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit61942a4

Browse files
authored
Merge pull request#299 from photostorm/net-addr
Returns real remote and local address instead mocked
2 parents9873274 +fecf26c commit61942a4

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

‎conn_test.go‎

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

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

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

‎netconn.go‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ import (
3333
// where only the reading/writing goroutines are interrupted but the connection
3434
// is kept alive.
3535
//
36-
// The Addr methods will return a mock net.Addr that returns "websocket" for Network
37-
// and "websocket/unknown-addr" for String.
36+
// The Addr methods will return the real addresses for connections obtained
37+
// from websocket.Accept. But for connections obtained from websocket.Dial, a mock net.Addr
38+
// will be returned that gives "websocket" for Network() and "websocket/unknown-addr" for
39+
// String(). This is because websocket.Dial only exposes a io.ReadWriteCloser instead of the
40+
// full net.Conn to us.
41+
//
42+
// When running as WASM, the Addr methods will always return the mock address described above.
3843
//
3944
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
4045
// io.EOF when reading.
@@ -181,14 +186,6 @@ func (a websocketAddr) String() string {
181186
return"websocket/unknown-addr"
182187
}
183188

184-
func (nc*netConn)RemoteAddr() net.Addr {
185-
returnwebsocketAddr{}
186-
}
187-
188-
func (nc*netConn)LocalAddr() net.Addr {
189-
returnwebsocketAddr{}
190-
}
191-
192189
func (nc*netConn)SetDeadline(t time.Time)error {
193190
nc.SetWriteDeadline(t)
194191
nc.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 (nc*netConn)RemoteAddr() net.Addr {
6+
returnwebsocketAddr{}
7+
}
8+
9+
func (nc*netConn)LocalAddr() net.Addr {
10+
returnwebsocketAddr{}
11+
}

‎netconn_notjs.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !js
2+
// +build !js
3+
4+
package websocket
5+
6+
import"net"
7+
8+
func (nc*netConn)RemoteAddr() net.Addr {
9+
ifunc,ok:=nc.c.rwc.(net.Conn);ok {
10+
returnunc.RemoteAddr()
11+
}
12+
returnwebsocketAddr{}
13+
}
14+
15+
func (nc*netConn)LocalAddr() net.Addr {
16+
ifunc,ok:=nc.c.rwc.(net.Conn);ok {
17+
returnunc.LocalAddr()
18+
}
19+
returnwebsocketAddr{}
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp