proto-socket/go/heartbeat_timer.go
toki 14e4cd56d0 feat: add WebSocket/WSS support and Go implementation
- Add WebSocket binary frame support to protocol specification
- Update README and PROTOCOL.md to reflect dual TCP/WebSocket transport
- Add Go implementation with TCP, WebSocket, and heartbeat support
- Include .claude settings configuration
2026-04-11 08:33:46 +09:00

38 lines
569 B
Go

package toki_socket
import (
"sync"
"time"
)
type HeartbeatTimer struct {
mu sync.Mutex
timer *time.Timer
callback func()
}
func NewHeartbeatTimer(d time.Duration, cb func()) *HeartbeatTimer {
h := &HeartbeatTimer{callback: cb}
h.Reset(d)
return h
}
func (h *HeartbeatTimer) Reset(d time.Duration) {
h.mu.Lock()
defer h.mu.Unlock()
if h.timer != nil {
h.timer.Stop()
}
h.timer = time.AfterFunc(d, h.callback)
}
func (h *HeartbeatTimer) Stop() {
h.mu.Lock()
defer h.mu.Unlock()
if h.timer != nil {
h.timer.Stop()
h.timer = nil
}
}