1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 14:11:59 +01:00

Add proxying for /audio to localhost:8081

This commit is contained in:
Mark de Bruijn
2024-02-19 20:58:08 +01:00
parent 4b766ec0e6
commit b87a286b83

View File

@@ -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')));