@@ -2,6 +2,7 @@ package websocket
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"io"
6
7
"math"
7
8
"net"
@@ -17,8 +18,8 @@ import (
17
18
// correctly and so provided in the library.
18
19
// See https://github.com/nhooyr/websocket/issues/100.
19
20
//
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.
22
23
//
23
24
// Close will close the *websocket.Conn with StatusNormalClosure.
24
25
//
@@ -30,9 +31,10 @@ import (
30
31
// and "websocket/unknown-addr" for String.
31
32
//
32
33
// A received StatusNormalClosure close frame will be translated to EOF when reading.
33
- func NetConn (c * Conn ) net.Conn {
34
+ func NetConn (c * Conn , msgType MessageType ) net.Conn {
34
35
nc := & netConn {
35
- c : c ,
36
+ c : c ,
37
+ msgType : msgType ,
36
38
}
37
39
38
40
var cancel context.CancelFunc
@@ -52,7 +54,8 @@ func NetConn(c *Conn) net.Conn {
52
54
}
53
55
54
56
type netConn struct {
55
- c * Conn
57
+ c * Conn
58
+ msgType MessageType
56
59
57
60
writeTimer * time.Timer
58
61
writeContext context.Context
@@ -71,7 +74,7 @@ func (c *netConn) Close() error {
71
74
}
72
75
73
76
func (c * netConn ) Write (p []byte ) (int , error ) {
74
- err := c .c .Write (c .writeContext , MessageBinary , p )
77
+ err := c .c .Write (c .writeContext , c . msgType , p )
75
78
if err != nil {
76
79
return 0 , err
77
80
}
@@ -93,9 +96,9 @@ func (c *netConn) Read(p []byte) (int, error) {
93
96
}
94
97
return 0 , err
95
98
}
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 )
99
+ if typ != c . msgType {
100
+ c .c .Close (StatusUnsupportedData , fmt . Sprintf ( "can only accept %v messages" , c . msgType ) )
101
+ return 0 , xerrors .Errorf ("unexpected frame type read for net conn adapter (expected %v): %v" , c . msgType , typ )
99
102
}
100
103
c .reader = r
101
104
}
0 commit comments