Skip to content

Commit 3586c61

Browse files
authored
Merge pull request #114 from nhooyr/netconn
Add msgType parameter to NetConn adapter
2 parents 5028f22 + 9228912 commit 3586c61

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

netconn.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package websocket
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"math"
78
"net"
@@ -17,8 +18,11 @@ import (
1718
// correctly and so provided in the library.
1819
// See https://github.com/nhooyr/websocket/issues/100.
1920
//
20-
// Every Write to the net.Conn will correspond to a binary message
21-
// write on *webscoket.Conn.
21+
// Every Write to the net.Conn will correspond to a message write of
22+
// the given type on *websocket.Conn.
23+
//
24+
// If a message is read that is not of the correct type, an error
25+
// will be thrown.
2226
//
2327
// Close will close the *websocket.Conn with StatusNormalClosure.
2428
//
@@ -30,9 +34,10 @@ import (
3034
// and "websocket/unknown-addr" for String.
3135
//
3236
// A received StatusNormalClosure close frame will be translated to EOF when reading.
33-
func NetConn(c *Conn) net.Conn {
37+
func NetConn(c *Conn, msgType MessageType) net.Conn {
3438
nc := &netConn{
35-
c: c,
39+
c: c,
40+
msgType: msgType,
3641
}
3742

3843
var cancel context.CancelFunc
@@ -52,7 +57,8 @@ func NetConn(c *Conn) net.Conn {
5257
}
5358

5459
type netConn struct {
55-
c *Conn
60+
c *Conn
61+
msgType MessageType
5662

5763
writeTimer *time.Timer
5864
writeContext context.Context
@@ -71,7 +77,7 @@ func (c *netConn) Close() error {
7177
}
7278

7379
func (c *netConn) Write(p []byte) (int, error) {
74-
err := c.c.Write(c.writeContext, MessageBinary, p)
80+
err := c.c.Write(c.writeContext, c.msgType, p)
7581
if err != nil {
7682
return 0, err
7783
}
@@ -93,9 +99,9 @@ func (c *netConn) Read(p []byte) (int, error) {
9399
}
94100
return 0, err
95101
}
96-
if typ != MessageBinary {
97-
c.c.Close(StatusUnsupportedData, "can only accept binary messages")
98-
return 0, xerrors.Errorf("unexpected frame type read for net conn adapter (expected %v): %v", MessageBinary, typ)
102+
if typ != c.msgType {
103+
c.c.Close(StatusUnsupportedData, fmt.Sprintf("can only accept %v messages", c.msgType))
104+
return 0, xerrors.Errorf("unexpected frame type read for net conn adapter (expected %v): %v", c.msgType, typ)
99105
}
100106
c.reader = r
101107
}

websocket.go

+4
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
333333
// This applies to the Read methods in the wsjson/wspb subpackages as well.
334334
//
335335
// You must read from the connection for control frames to be handled.
336+
// Thus if you expect messages to take a long time to be responded to,
337+
// you should handle such messages async to reading from the connection
338+
// to ensure control frames are promptly handled.
339+
//
336340
// If you do not expect any data messages from the peer, call CloseRead.
337341
//
338342
// Only one Reader may be open at a time.

websocket_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestHandshake(t *testing.T) {
127127
}
128128
defer c.Close(websocket.StatusInternalError, "")
129129

130-
nc := websocket.NetConn(c)
130+
nc := websocket.NetConn(c, websocket.MessageBinary)
131131
defer nc.Close()
132132

133133
nc.SetWriteDeadline(time.Time{})
@@ -152,7 +152,7 @@ func TestHandshake(t *testing.T) {
152152
}
153153
defer c.Close(websocket.StatusInternalError, "")
154154

155-
nc := websocket.NetConn(c)
155+
nc := websocket.NetConn(c, websocket.MessageBinary)
156156
defer nc.Close()
157157

158158
nc.SetReadDeadline(time.Time{})

0 commit comments

Comments
 (0)