add server code
This commit is contained in:
parent
c39fd40be4
commit
87bf90ebf2
4 changed files with 72 additions and 3 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
node_modules/
|
||||
.env
|
24
package-lock.json
generated
24
package-lock.json
generated
|
@ -9,7 +9,8 @@
|
|||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^5.1.0"
|
||||
"express": "^5.1.0",
|
||||
"ws": "^8.18.2"
|
||||
}
|
||||
},
|
||||
"node_modules/accepts": {
|
||||
|
@ -811,6 +812,27 @@
|
|||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.18.2",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.2.tgz",
|
||||
"integrity": "sha512-DMricUmwGZUVr++AEAe2uiVM7UoO9MAVZMDu05UQOaUII0lp+zOzLLU4Xqh/JvTqklB1T4uELaaPBKyjE1r4fQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": ">=5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "ai-tutor-proxy",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
|
@ -13,6 +13,7 @@
|
|||
"license": "MIT",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"express": "^5.1.0"
|
||||
"express": "^5.1.0",
|
||||
"ws": "^8.18.2"
|
||||
}
|
||||
}
|
||||
|
|
44
server.js
Normal file
44
server.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
// server.js
|
||||
import express from 'express';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { createServer } from 'http';
|
||||
import WebSocket from 'ws';
|
||||
|
||||
const app = express();
|
||||
const server = createServer(app);
|
||||
const wss = new WebSocketServer({ server });
|
||||
|
||||
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
|
||||
|
||||
wss.on('connection', (clientWs) => {
|
||||
const openaiWs = new WebSocket('wss://api.openai.com/v1/realtime', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${OPENAI_API_KEY}`
|
||||
}
|
||||
});
|
||||
|
||||
openaiWs.on('open', () => {
|
||||
console.log('Connected to OpenAI');
|
||||
});
|
||||
|
||||
// Proxy messages from client to OpenAI
|
||||
clientWs.on('message', (message) => {
|
||||
if (openaiWs.readyState === WebSocket.OPEN) {
|
||||
openaiWs.send(message);
|
||||
}
|
||||
});
|
||||
|
||||
// Proxy messages from OpenAI back to client
|
||||
openaiWs.on('message', (message) => {
|
||||
if (clientWs.readyState === WebSocket.OPEN) {
|
||||
clientWs.send(message);
|
||||
}
|
||||
});
|
||||
|
||||
openaiWs.on('close', () => clientWs.close());
|
||||
clientWs.on('close', () => openaiWs.close());
|
||||
});
|
||||
|
||||
server.listen(3001, () => {
|
||||
console.log('WebSocket proxy listening on ws://localhost:3001');
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue