- 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
38 lines
569 B
Go
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
|
|
}
|
|
}
|