Use net.Buffers if they are available.

Closes #346.
This commit is contained in:
Alexey Palazhchenko 2018-02-19 11:37:33 +03:00
parent f37d158860
commit 1cbd7e9b31
3 changed files with 35 additions and 8 deletions

View File

@ -382,15 +382,9 @@ func (c *Conn) write(frameType int, deadline time.Time, bufs ...[]byte) error {
} }
c.conn.SetWriteDeadline(deadline) c.conn.SetWriteDeadline(deadline)
for _, buf := range bufs { if err = c.writeBufs(bufs); err != nil {
if len(buf) > 0 {
_, err := c.conn.Write(buf)
if err != nil {
return c.writeFatal(err) return c.writeFatal(err)
} }
}
}
if frameType == CloseMessage { if frameType == CloseMessage {
c.writeFatal(ErrCloseSent) c.writeFatal(ErrCloseSent)
} }

15
conn_write.go Normal file
View File

@ -0,0 +1,15 @@
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.8
package websocket
import "net"
func (c *Conn) writeBufs(bufs [][]byte) error {
b := net.Buffers(bufs)
_, err := b.WriteTo(c.conn)
return err
}

18
conn_write_legacy.go Normal file
View File

@ -0,0 +1,18 @@
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !go1.8
package websocket
func (c *Conn) writeBufs(bufs [][]byte) error {
for _, buf := range bufs {
if len(buf) > 0 {
if _, err := c.conn.Write(buf); err != nil {
return err
}
}
}
return nil
}