+
90
-

cloudflare worker如何实现openai的realtime api websocket代理?

cloudflare worker如何实现openai的realtime api websocket代理?


网友回复

+
4
-

代码如下:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  if (request.headers.get('Upgrade') !== 'websocket') {
    return new Response('Expected Upgrade: websocket', { status: 426 })
  }

  const targetUrl = 'wss://openai websocket 的api网关地址'
  const [client, server] = Object.values(new WebSocketPair())

  await server.accept()

  const targetWebSocket = new WebSocket(targetUrl)

  targetWebSocket.addEventListener('open', () => {
    console.log('Connected to target WebSocket server')
  })

  server.addEven...

点击查看剩余70%

我知道答案,我要回答