diff --git a/nbhttp/websocket/conn.go b/nbhttp/websocket/conn.go index 8c3f50da..ce5e3ccd 100644 --- a/nbhttp/websocket/conn.go +++ b/nbhttp/websocket/conn.go @@ -608,6 +608,21 @@ func (c *Conn) WriteMessage(messageType MessageType, data []byte) error { return c.writeFrame(messageType, true, true, []byte{}, compress) } +// Keepalive . +func (c *Conn) Keepalive(d time.Duration) *time.Timer { + var fn func() + var timer *time.Timer + fn = func() { + err := c.WriteMessage(PingMessage, []byte{}) + if err != nil { + return + } + timer.Reset(d) + } + timer = time.AfterFunc(d, fn) + return timer +} + // Session returns user session. func (c *Conn) Session() interface{} { if c.chSessionInited == nil { diff --git a/protocol_stack.go b/protocol_stack.go index f0267858..cb6a2c36 100644 --- a/protocol_stack.go +++ b/protocol_stack.go @@ -18,11 +18,18 @@ func (ps *ProtocolStack) Add(p Protocol) { } func (ps *ProtocolStack) Delete(p Protocol) { - for i, v := range ps.stack { - if v == p { + i := len(ps.stack) - 1 + for i >= 0 { + if ps.stack[i] == p { ps.stack[i] = nil + if i+1 > len(ps.stack)-1 { + ps.stack = ps.stack[:i] + } else { + ps.stack = append(ps.stack[:i], ps.stack[i+1:]...) + } return } + i-- } }