Skip to content

Commit

Permalink
feat: support websocket ping
Browse files Browse the repository at this point in the history
  • Loading branch information
WqyJh committed Feb 8, 2025
1 parent 138a4af commit 294980c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type mockWebSocketConn struct {
writeMessageFunc func(ctx context.Context, messageType openairt.MessageType, data []byte) error
closeFunc func() error
responseFunc func() *http.Response
pingFunc func(ctx context.Context) error
}

func (m *mockWebSocketConn) ReadMessage(ctx context.Context) (openairt.MessageType, []byte, error) {
Expand All @@ -40,6 +41,10 @@ func (m *mockWebSocketConn) Response() *http.Response {
return m.responseFunc()
}

func (m *mockWebSocketConn) Ping(ctx context.Context) error {
return m.pingFunc(ctx)
}

func TestConnect(t *testing.T) {
token := "mock-token"
model := "test-model"
Expand Down
3 changes: 3 additions & 0 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type WebSocketConn interface {
// Response returns the *http.Response of the WebSocket connection.
// Commonly used to get response headers.
Response() *http.Response

// Ping sends a ping message to the WebSocket connection.
Ping(ctx context.Context) error
}

// WebSocketDialer is a WebSocket connection dialer abstraction.
Expand Down
5 changes: 5 additions & 0 deletions ws_coder.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,8 @@ func (c *CoderWebSocketConn) Close() error {
func (c *CoderWebSocketConn) Response() *http.Response {
return c.resp
}

// Ping sends a ping message to the WebSocket connection.
func (c *CoderWebSocketConn) Ping(ctx context.Context) error {
return c.conn.Ping(ctx)
}

0 comments on commit 294980c

Please # to comment.