mirror of
https://github.com/netbirdio/netbird.git
synced 2025-01-20 21:08:45 +01:00
56 lines
796 B
Go
56 lines
796 B
Go
|
package ws
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gorilla/websocket"
|
||
|
)
|
||
|
|
||
|
type Conn struct {
|
||
|
*websocket.Conn
|
||
|
}
|
||
|
|
||
|
func NewConn(wsConn *websocket.Conn) net.Conn {
|
||
|
return &Conn{
|
||
|
wsConn,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *Conn) Read(b []byte) (n int, err error) {
|
||
|
t, r, err := c.NextReader()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
if t != websocket.BinaryMessage {
|
||
|
return 0, fmt.Errorf("unexpected message type")
|
||
|
}
|
||
|
|
||
|
return r.Read(b)
|
||
|
}
|
||
|
|
||
|
func (c *Conn) Write(b []byte) (int, error) {
|
||
|
err := c.WriteMessage(websocket.BinaryMessage, b)
|
||
|
return len(b), err
|
||
|
}
|
||
|
|
||
|
func (c *Conn) SetDeadline(t time.Time) error {
|
||
|
errR := c.SetReadDeadline(t)
|
||
|
errW := c.SetWriteDeadline(t)
|
||
|
|
||
|
if errR != nil {
|
||
|
return errR
|
||
|
}
|
||
|
|
||
|
if errW != nil {
|
||
|
return errW
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (c *Conn) Close() error {
|
||
|
return c.Conn.Close()
|
||
|
}
|