-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
39 lines (35 loc) · 1.08 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 等待 init.js 中的连接建立后再执行
let dc;
window.setupDataChannel = function(peerConnection) {
// Create a data channel from a peer connection
dc = peerConnection.createDataChannel("oai-events");
// Listen for server-sent events on the data channel
dc.addEventListener("message", (e) => {
try {
const realtimeEvent = JSON.parse(e.data);
console.log(realtimeEvent);
} catch (error) {
console.error('Error parsing message:', error);
}
});
};
// 发送消息的函数
window.sendMessage = function() {
if (!dc) {
console.error('Data channel not established yet');
return;
}
// Send client events by serializing a valid client event to JSON
const responseCreate = {
type: "response.create",
response: {
modalities: ["text"],
instructions: "Write a haiku about code",
},
};
try {
dc.send(JSON.stringify(responseCreate));
} catch (error) {
console.error('Error sending message:', error);
}
};