packagec conn object and add README

This commit is contained in:
ye wang
2025-02-20 19:53:05 +08:00
parent daab9fdc68
commit 49f3afc111
3 changed files with 26 additions and 11 deletions
+6
View File
@@ -138,3 +138,9 @@ func main() {
)
}
```
# ADDATIONS
### The conditions for WebSocket disconnection
1. If task execution fails, the server sends the task-failed event and closes the websocketConnection.
2. If the interval between two tasks exceeds 60 seconds, the connection will be disconnected due to timeout. In this package, after the Asynthesizer/Synthesizer object is created, a goroutine is started for health detection by send pingMessage. Developers don't need to worry about this condition.
3. In task, if the interval between two commands exceeds 23S, the server triggers a timeout error and disconnects. When you use Asyncthesizer, if your business is likely to exceed 23S, periodically call SendText() to send empty characters or punctuation marks to prevent the connection breaking. Synthesizer is not needed.
+6 -10
View File
@@ -95,16 +95,12 @@ func (c *Client) AsyncSynthesizer(ctx context.Context, opts ...SynthesizerOption
return nil, err
}
ctx, cancel := context.WithCancel(ctx)
conn := &wsConn{
ctx: ctx,
cancel: cancel,
conn: socketConn,
logger: option.logger,
pingInterval: time.Duration(option.pingInterval) * time.Second,
ticker: time.NewTicker(time.Duration(option.pingInterval) * time.Second),
}
conn := newWsConn(
ctx,
socketConn,
option.logger,
time.Duration(option.pingInterval)*time.Second,
)
go conn.handleHealthCheck()
+14 -1
View File
@@ -17,13 +17,26 @@ type wsConn struct {
conn openairt.WebSocketConn
logger openairt.Logger
pingInterval time.Duration
ticker *time.Ticker
ticker *time.Ticker
lock sync.Mutex
wg sync.WaitGroup
sendBarrier chan struct{}
}
func newWsConn(ctx context.Context, conn openairt.WebSocketConn, logger openairt.Logger, pingInterval time.Duration) *wsConn {
ctx, cancel := context.WithCancel(ctx)
return &wsConn{
ctx: ctx,
cancel: cancel,
conn: conn,
logger: logger,
pingInterval: pingInterval,
ticker: time.NewTicker(pingInterval),
}
}
type Task struct {
data string
}