File tree 10 files changed +60
-8
lines changed
10 files changed +60
-8
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,11 @@ func (c *Conn) ReadMessage(ctx context.Context) (ServerEvent, error) {
58
58
return event , nil
59
59
}
60
60
61
+ // Ping sends a ping message to the WebSocket connection.
62
+ func (c * Conn ) Ping (ctx context.Context ) error {
63
+ return c .conn .Ping (ctx )
64
+ }
65
+
61
66
// ConnHandler is a handler for a connection to the OpenAI Realtime API.
62
67
// It reads messages from the server in a standalone goroutine and calls the registered handlers.
63
68
// It is the responsibility of the caller to call Start and Stop.
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ type mockWebSocketConn struct {
22
22
writeMessageFunc func (ctx context.Context , messageType openairt.MessageType , data []byte ) error
23
23
closeFunc func () error
24
24
responseFunc func () * http.Response
25
+ pingFunc func (ctx context.Context ) error
25
26
}
26
27
27
28
func (m * mockWebSocketConn ) ReadMessage (ctx context.Context ) (openairt.MessageType , []byte , error ) {
@@ -40,6 +41,10 @@ func (m *mockWebSocketConn) Response() *http.Response {
40
41
return m .responseFunc ()
41
42
}
42
43
44
+ func (m * mockWebSocketConn ) Ping (ctx context.Context ) error {
45
+ return m .pingFunc (ctx )
46
+ }
47
+
43
48
func TestConnect (t * testing.T ) {
44
49
token := "mock-token"
45
50
model := "test-model"
Original file line number Diff line number Diff line change 5
5
// replace github.com/WqyJh/go-openai-realtime => ../../
6
6
7
7
require (
8
- github.com/WqyJh/go-openai-realtime v0.2.0-beta.2
8
+ github.com/WqyJh/go-openai-realtime v0.3.5-0.20250208091501-af3079f3fb2b
9
9
github.com/gorilla/websocket v1.5.3
10
10
github.com/stretchr/testify v1.9.0
11
11
)
@@ -14,6 +14,5 @@ require (
14
14
github.com/coder/websocket v1.8.12 // indirect
15
15
github.com/davecgh/go-spew v1.1.1 // indirect
16
16
github.com/pmezard/go-difflib v1.0.0 // indirect
17
- github.com/sashabaranov/go-openai v1.32.0 // indirect
18
17
gopkg.in/yaml.v3 v3.0.1 // indirect
19
18
)
Original file line number Diff line number Diff line change 1
- github.com/WqyJh/go-openai-realtime v0.2.0-beta.2 h1:4xD7O/IN5dQD2gKsiPzP1Prdu5HtCy25CChXep9kaIs =
2
- github.com/WqyJh/go-openai-realtime v0.2.0-beta.2 /go.mod h1:nmUYODacQuObuh0DFSEIDfJM9L4ZFDRU/OEGYMryt60 =
1
+ github.com/WqyJh/go-openai-realtime v0.3.5-0.20250208091501-af3079f3fb2b h1:8teuz/JaWkMaqBrlIKF86EhHi0ajbJrm+shD1IXCvK8 =
2
+ github.com/WqyJh/go-openai-realtime v0.3.5-0.20250208091501-af3079f3fb2b /go.mod h1:nmUYODacQuObuh0DFSEIDfJM9L4ZFDRU/OEGYMryt60 =
3
3
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo =
4
4
github.com/coder/websocket v1.8.12 /go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs =
5
5
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c =
@@ -9,7 +9,6 @@ github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
9
9
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
10
10
github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
11
11
github.com/sashabaranov/go-openai v1.32.0 h1:Yk3iE9moX3RBXxrof3OBtUBrE7qZR0zF9ebsoO4zVzI =
12
- github.com/sashabaranov/go-openai v1.32.0 /go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg =
13
12
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg =
14
13
github.com/stretchr/testify v1.9.0 /go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY =
15
14
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM =
Original file line number Diff line number Diff line change @@ -118,3 +118,14 @@ func (c *WebSocketConn) Close() error {
118
118
func (c * WebSocketConn ) Response () * http.Response {
119
119
return c .resp
120
120
}
121
+
122
+ // Ping sends a ping message to the WebSocket connection.
123
+ // It won't be blocked until the pong message is received.
124
+ func (c * WebSocketConn ) Ping (ctx context.Context ) error {
125
+ deadline , ok := ctx .Deadline ()
126
+ if ok {
127
+ _ = c .conn .SetWriteDeadline (deadline )
128
+ }
129
+
130
+ return c .conn .WriteControl (websocket .PingMessage , []byte {}, deadline )
131
+ }
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ module github.com/WqyJh/go-openai-realtime/examples
3
3
go 1.19
4
4
5
5
require (
6
- github.com/WqyJh/go-openai-realtime v0.3.0-alpha
6
+ github.com/WqyJh/go-openai-realtime v0.3.5-0.20250208091501-af3079f3fb2b
7
7
github.com/faiface/beep v1.1.0
8
8
github.com/gordonklaus/portaudio v0.0.0-20230709114228-aafa478834f5
9
9
github.com/sashabaranov/go-openai v1.32.0
Original file line number Diff line number Diff line change 1
1
github.com/DATA-DOG/go-sqlmock v1.3.3 /go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM =
2
- github.com/WqyJh/go-openai-realtime v0.3.0-alpha h1:2IDh9xm3VLAXnmOtbOhv2/ytMceJYgj7Cw0e1TbqBpY =
3
- github.com/WqyJh/go-openai-realtime v0.3.0-alpha /go.mod h1:nmUYODacQuObuh0DFSEIDfJM9L4ZFDRU/OEGYMryt60 =
2
+ github.com/WqyJh/go-openai-realtime v0.3.5-0.20250208091501-af3079f3fb2b h1:8teuz/JaWkMaqBrlIKF86EhHi0ajbJrm+shD1IXCvK8 =
3
+ github.com/WqyJh/go-openai-realtime v0.3.5-0.20250208091501-af3079f3fb2b /go.mod h1:nmUYODacQuObuh0DFSEIDfJM9L4ZFDRU/OEGYMryt60 =
4
4
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo =
5
5
github.com/coder/websocket v1.8.12 /go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs =
6
6
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b /go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo =
Original file line number Diff line number Diff line change 6
6
"fmt"
7
7
"log"
8
8
"os"
9
+ "sync"
10
+ "time"
9
11
10
12
openairt "github.com/WqyJh/go-openai-realtime"
11
13
)
@@ -19,6 +21,8 @@ func main() {
19
21
log .Fatal (err )
20
22
}
21
23
24
+ var sendLock sync.Mutex
25
+
22
26
// Teletype response
23
27
responseDeltaHandler := func (ctx context.Context , event openairt.ServerEvent ) {
24
28
switch event .ServerEventType () {
@@ -48,6 +52,24 @@ func main() {
48
52
log .Fatal (err )
49
53
}
50
54
55
+ go func () {
56
+ for {
57
+ select {
58
+ case <- ctx .Done ():
59
+ return
60
+ case <- time .After (30 * time .Second ):
61
+ sendLock .Lock ()
62
+ err := conn .Ping (ctx )
63
+ sendLock .Unlock ()
64
+ if err != nil {
65
+ log .Printf ("ping error: %v" , err )
66
+ } else {
67
+ log .Printf ("ping success" )
68
+ }
69
+ }
70
+ }
71
+ }()
72
+
51
73
fmt .Println ("Conversation" )
52
74
fmt .Println ("---------------------" )
53
75
fmt .Print ("> " )
@@ -56,6 +78,7 @@ func main() {
56
78
if s .Text () == "exit" || s .Text () == "quit" {
57
79
break
58
80
}
81
+ sendLock .Lock ()
59
82
conn .SendMessage (ctx , & openairt.ConversationItemCreateEvent {
60
83
Item : openairt.MessageItem {
61
84
ID : openairt .GenerateID ("msg_" , 10 ),
@@ -76,6 +99,7 @@ func main() {
76
99
MaxOutputTokens : 4000 ,
77
100
},
78
101
})
102
+ sendLock .Unlock ()
79
103
}
80
104
conn .Close ()
81
105
err = <- connHandler .Err ()
Original file line number Diff line number Diff line change @@ -54,6 +54,9 @@ type WebSocketConn interface {
54
54
// Response returns the *http.Response of the WebSocket connection.
55
55
// Commonly used to get response headers.
56
56
Response () * http.Response
57
+
58
+ // Ping sends a ping message to the WebSocket connection.
59
+ Ping (ctx context.Context ) error
57
60
}
58
61
59
62
// WebSocketDialer is a WebSocket connection dialer abstraction.
Original file line number Diff line number Diff line change @@ -128,3 +128,9 @@ func (c *CoderWebSocketConn) Close() error {
128
128
func (c * CoderWebSocketConn ) Response () * http.Response {
129
129
return c .resp
130
130
}
131
+
132
+ // Ping sends a ping message to the WebSocket connection.
133
+ // It would be blocked until the pong message is received or the ctx is done.
134
+ func (c * CoderWebSocketConn ) Ping (ctx context.Context ) error {
135
+ return c .conn .Ping (ctx )
136
+ }
You can’t perform that action at this time.
0 commit comments