Skip to content

Commit

Permalink
websocket: add Conn.Keepalive(d)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Sep 22, 2024
1 parent 3737403 commit 7167510
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions nbhttp/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions protocol_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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--
}
}

Expand Down

0 comments on commit 7167510

Please sign in to comment.