package net import ( "time" "github.com/gorilla/websocket" ) type wsConn struct { conn *websocket.Conn } func newWSConn(conn *websocket.Conn) *wsConn { // Bound incoming message size so a client cannot push arbitrarily large // frames into memory before the session's truncation logic runs. conn.SetReadLimit(maxLineLength * 4) return &wsConn{conn: conn} } func (w *wsConn) ReadMessage() (string, error) { _, msg, err := w.conn.ReadMessage() return string(msg), err } func (w *wsConn) Write(b []byte) (int, error) { err := w.conn.WriteMessage(websocket.TextMessage, b) return len(b), err } // RemoteAddr reports the peer address for the login throttle. func (w *wsConn) RemoteAddr() string { if w.conn == nil { return "" } return w.conn.RemoteAddr().String() } // SetWriteDeadline delegates to the underlying websocket connection, which // supports deadlines natively. func (w *wsConn) SetWriteDeadline(ts time.Time) error { return w.conn.SetWriteDeadline(ts) } func (w *wsConn) Close() error { return w.conn.Close() } func (w *wsConn) SetEcho(on bool) error { return w.conn.WriteMessage(websocket.TextMessage, w.EchoSequence(on)) } // EchoSequence returns the browser echo-control bytes for the websocket // transport; queued sessions send these through the session writer so a // stalled browser cannot block the caller (see Session.SetEcho). func (w *wsConn) EchoSequence(on bool) []byte { if on { return []byte("\x1b]10;0\x07") } return []byte("\x1b]10;1\x07") } // SetEchoState is a no-op: the websocket transport keeps no local echo state. func (w *wsConn) SetEchoState(bool) {}