2024-06-28 11:17:06 +02:00
|
|
|
package ws
|
2024-06-03 21:38:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"nhooyr.io/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Conn struct {
|
|
|
|
ctx context.Context
|
2024-06-19 18:16:23 +02:00
|
|
|
*websocket.Conn
|
2024-07-16 11:02:32 +02:00
|
|
|
remoteAddr WebsocketAddr
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:02:32 +02:00
|
|
|
func NewConn(wsConn *websocket.Conn, serverAddress string) net.Conn {
|
2024-06-03 21:38:37 +02:00
|
|
|
return &Conn{
|
2024-07-16 11:02:32 +02:00
|
|
|
ctx: context.Background(),
|
|
|
|
Conn: wsConn,
|
|
|
|
remoteAddr: WebsocketAddr{serverAddress},
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Read(b []byte) (n int, err error) {
|
|
|
|
t, ioReader, err := c.Conn.Reader(c.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t != websocket.MessageBinary {
|
|
|
|
return 0, fmt.Errorf("unexpected message type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioReader.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Write(b []byte) (n int, err error) {
|
|
|
|
err = c.Conn.Write(c.ctx, websocket.MessageBinary, b)
|
2024-07-22 13:13:12 +02:00
|
|
|
return 0, err
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) RemoteAddr() net.Addr {
|
2024-07-16 11:02:32 +02:00
|
|
|
return c.remoteAddr
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) LocalAddr() net.Addr {
|
2024-07-16 11:02:32 +02:00
|
|
|
return WebsocketAddr{addr: "unknown"}
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
2024-07-09 16:29:38 +02:00
|
|
|
return fmt.Errorf("SetReadDeadline is not implemented")
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
2024-07-09 16:29:38 +02:00
|
|
|
return fmt.Errorf("SetWriteDeadline is not implemented")
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) SetDeadline(t time.Time) error {
|
2024-07-09 16:29:38 +02:00
|
|
|
return fmt.Errorf("SetDeadline is not implemented")
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Close() error {
|
2024-06-09 12:41:52 +02:00
|
|
|
return c.Conn.CloseNow()
|
2024-06-03 21:38:37 +02:00
|
|
|
}
|