From b87a286b83c4b127ba6f3aeec1db17eeca65f6d5 Mon Sep 17 00:00:00 2001 From: Mark de Bruijn Date: Mon, 19 Feb 2024 20:58:08 +0100 Subject: [PATCH] Add proxying for /audio to localhost:8081 --- index.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c6eae03..3e2d69f 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const express = require('express'); const session = require('express-session'); const bodyParser = require('body-parser'); const http = require('http'); +const httpProxy = require('http-proxy'); const https = require('https'); const app = express(); const httpServer = http.createServer(app); @@ -18,6 +19,13 @@ const path = require('path'); const net = require('net'); const client = new net.Socket(); +// Create a WebSocket proxy instance +const proxy = httpProxy.createProxyServer({ + target: 'ws://localhost:8081', // WebSocket httpServer's address + ws: true, // Enable WebSocket proxying + changeOrigin: true // Change the origin of the host header to the target URL +}); + // Other files and libraries const crypto = require('crypto'); const fs = require('fs'); @@ -428,14 +436,23 @@ wss.on('connection', (ws, request) => { ws.on('error', console.error); }); - +// Handle upgrade requests to proxy WebSocket connections httpServer.on('upgrade', (request, socket, head) => { - sessionMiddleware(request, {}, () => { - wss.handleUpgrade(request, socket, head, (ws) => { - wss.emit('connection', ws, request); + + logInfo(request.url); + if (request.url === '/text') { + sessionMiddleware(request, {}, () => { + wss.handleUpgrade(request, socket, head, (ws) => { + wss.emit('connection', ws, request); + }); }); - }); -}); + } else if (request.url === '/audio/') { + proxy.ws(request, socket, head); + } else { + socket.destroy(); + } +} +); /* Serving of HTML files */ app.use(express.static(path.join(__dirname, 'web')));