Websocket Fixes & New Config Examples
This commit is contained in:
parent
84225a66f9
commit
9bcdc532bb
10 changed files with 389 additions and 96 deletions
34
index.js
34
index.js
|
|
@ -6,7 +6,6 @@ import { secureImportModule } from './utils/plugins.js';
|
|||
import * as logs from './utils/logs.js';
|
||||
import express from 'express';
|
||||
import { createServer } from 'http';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
// Load environment variables from .env file
|
||||
|
|
@ -135,9 +134,6 @@ async function main() {
|
|||
// Trust proxy headers (important for proper protocol detection)
|
||||
app.set('trust proxy', true);
|
||||
|
||||
// Initialize WebSocket server
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
|
||||
try {
|
||||
await secureImportModule('checkpoint.js');
|
||||
} catch (e) {
|
||||
|
|
@ -203,12 +199,30 @@ async function main() {
|
|||
res.status(500).send(`Server Error: ${err.message}`);
|
||||
});
|
||||
|
||||
// Handle WebSocket upgrades for http-proxy-middleware
|
||||
server.on('upgrade', (request, socket, head) => {
|
||||
// http-proxy-middleware handles WebSocket upgrades internally
|
||||
// This is just a placeholder for any custom WebSocket handling if needed
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
wss.emit('connection', ws, request);
|
||||
// Handle WebSocket upgrades using http-proxy-middleware instances
|
||||
server.on('upgrade', (req, socket, head) => {
|
||||
const hostname = req.headers.host?.split(':')[0];
|
||||
if (!hostname) {
|
||||
logs.error('websocket', 'Upgrade request without host header, destroying socket.');
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
logs.server(`WebSocket upgrade request for ${hostname}${req.url}`);
|
||||
|
||||
import('./plugins/proxy.js').then(proxyModule => {
|
||||
const hpmInstance = proxyModule.getHpmInstance(hostname);
|
||||
|
||||
if (hpmInstance && typeof hpmInstance.upgrade === 'function') {
|
||||
logs.server(`Attempting to upgrade WebSocket for ${hostname} using HPM instance.`);
|
||||
hpmInstance.upgrade(req, socket, head);
|
||||
} else {
|
||||
logs.error('websocket', `No HPM instance or upgrade method found for ${hostname}${req.url}`);
|
||||
socket.destroy();
|
||||
}
|
||||
}).catch(err => {
|
||||
logs.error('websocket', `Error importing proxy module for upgrade: ${err.message}`);
|
||||
socket.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue