Skip to content

Commit 3842c68

Browse files
authored
新增NetConn接口 (#14)
* 新增NetConn接口 * 更新
1 parent aba7e1b commit 3842c68

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

common_options_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,7 @@ func Test_CommonOption(t *testing.T) {
15551555
t.Errorf("write message or read message fail:got:%s, need:hello\n", d)
15561556
}
15571557
case <-time.After(1000 * time.Millisecond):
1558+
t.Errorf("write message timeout\n")
15581559
}
15591560
if atomic.LoadInt32(&run) != 1 {
15601561
t.Error("not run server:method fail")

conn.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func newConn(c net.Conn, client bool, conf *Config, fr fixedreader.FixedReader,
9393
return con
9494
}
9595

96+
// 返回标准库的net.Conn
97+
func (c *Conn) NetConn() net.Conn {
98+
return c.c
99+
}
100+
96101
func (c *Conn) writeErrAndOnClose(code StatusCode, userErr error) error {
97102
defer c.Callback.OnClose(c, userErr)
98103
if err := c.WriteTimeout(opcode.Close, statusCodeToBytes(code), 2*time.Second); err != nil {

conn_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,43 @@ func Test_WriteControl(t *testing.T) {
623623
})
624624
}
625625

626+
func Test_API(t *testing.T) {
627+
t.Run("NetConn", func(t *testing.T) {
628+
var shandler testPingPongCloseHandler
629+
shandler.data = make(chan string, 1)
630+
upgrade := NewUpgrade(WithServerBufioParseMode(), WithServerCallback(&shandler))
631+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
632+
c, err := upgrade.Upgrade(w, r)
633+
if err != nil {
634+
t.Error(err)
635+
}
636+
if c.NetConn() != c.c {
637+
t.Error("server.not equal")
638+
}
639+
c.StartReadLoop()
640+
}))
641+
642+
defer ts.Close()
643+
644+
url := strings.ReplaceAll(ts.URL, "http", "ws")
645+
con, err := Dial(url, WithClientOnMessageFunc(func(c *Conn, mt Opcode, payload []byte) {
646+
}))
647+
if err != nil {
648+
t.Error(err)
649+
}
650+
defer con.Close()
651+
if con.NetConn() != con.c {
652+
t.Error("client.not equal")
653+
}
654+
655+
err = con.WriteControl(Close, bytes.Repeat([]byte{1}, 126))
656+
// 这里必须要报错
657+
if err == nil {
658+
t.Error("not error")
659+
}
660+
})
661+
}
662+
626663
// 测试ping pong close control信息
627664
func TestPingPongClose(t *testing.T) {
628665
// 写一个超过maxControlFrameSize的消息

0 commit comments

Comments
 (0)