完成http、https修改

This commit is contained in:
2026-05-17 22:07:07 +08:00
parent 7ff53c2cfd
commit a8ceb194f6
5 changed files with 38 additions and 20 deletions

View File

@@ -14,13 +14,21 @@ export class RenderStreaming {
public static run(argv: string[]): RenderStreaming {
const program = new Command();
const readOptions = (): Options => {
const parsePort = (value: string | number | undefined): number | undefined => {
if (value === undefined || value === null || value === '') {
return undefined;
}
const parsed = Number(value);
return Number.isNaN(parsed) ? undefined : parsed;
};
// 确保argv是数组
const args = Array.isArray(argv) ? argv : process.argv;
program
.usage('[options] <apps...>')
.option('-p, --port <n>', 'Port to start the server on.', process.env.PORT || `80`)
.option('-s, --secure', 'Enable HTTPS (you need server.key and server.cert).', process.env.SECURE || true)
.option('-p, --port <n>', 'Port to start the server on.', process.env.PORT || '8080')
.option('-s, --secure', 'Enable HTTPS (you need server.key and server.cert).')
.option('-k, --keyfile <path>', 'https key file.', process.env.KEYFILE || 'server.key')
.option('-c, --certfile <path>', 'https cert file.', process.env.CERTFILE || 'server.cert')
.option('-t, --type <type>', 'Type of signaling protocol, Choose websocket or http.', process.env.TYPE || 'websocket')
@@ -29,9 +37,11 @@ export class RenderStreaming {
.parse(args);
const option = program.opts();
const legacyPort = parsePort(option.port);
const secure = option.secure === true || process.env.SECURE === 'true';
return {
port: option.port,
secure: option.secure == undefined ? false : option.secure,
port: legacyPort ?? 8080,
secure,
keyfile: option.keyfile,
certfile: option.certfile,
type: option.type == undefined ? 'websocket' : option.type,
@@ -52,6 +62,7 @@ export class RenderStreaming {
constructor(options: Options) {
this.options = options;
this.app = createServer(this.options);
if (this.options.secure) {
this.server = https.createServer({
key: fs.readFileSync(options.keyfile),
@@ -81,9 +92,8 @@ export class RenderStreaming {
this.options.type = 'websocket';
}
if (this.options.type == 'websocket') {
log(LogLevel.info, `Use websocket for signaling server ws://${this.getIPAddress()[0]}`);
//Start Websocket Signaling server
const protocol = this.options.secure ? 'wss' : 'ws';
log(LogLevel.info, `Use websocket for signaling server ${protocol}://${this.getIPAddress()[0]}:${this.options.port}`);
new WSSignaling(this.server, this.options.mode);
}

View File

@@ -22,7 +22,13 @@ export const createServer = (config: Options): express.Express => {
app.use(cors({origin: '*'}));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/config', (req, res) => res.json({ useWebSocket: config.type == 'websocket', startupMode: config.mode, logging: config.logging }));
app.get('/config', (req, res) => res.json({
useWebSocket: config.type == 'websocket',
startupMode: config.mode,
logging: config.logging,
protocol: config.secure ? 'https' : 'http',
port: config.port
}));
app.use('/signaling', signaling);
app.use(express.static(path.join(__dirname, '../client/public')));
app.use('/module', express.static(path.join(__dirname, '../client/src')));

View File

@@ -14,10 +14,15 @@ import { log, LogLevel } from './log';
* @param config 配置选项
*/
export const initSwagger = (app: Express, config: Options): void => {
// 根据配置生成服务器URL
const protocol = config.secure ? 'https' : 'http';
const port = config.port || 8080;
const serverUrl = `${protocol}://localhost:${port}`;
const servers = [
{
url: serverUrl,
description: config.secure ? '本地HTTPS服务器' : '本地HTTP服务器'
}
];
/**
* Swagger配置选项
@@ -34,12 +39,7 @@ export const initSwagger = (app: Express, config: Options): void => {
email: 'contact@webrtc.example.com'
}
},
servers: [
{
url: serverUrl,
description: '本地开发服务器'
}
],
servers,
components: {
securitySchemes: {
sessionAuth: {

View File

@@ -1,10 +1,11 @@
import * as websocket from "ws";
import { Server } from 'http';
import { Server as HttpServer } from 'http';
import { Server as HttpsServer } from 'https';
import * as handler from "./class/websockethandler";
import { log, LogLevel } from './log';
export default class WSSignaling {
server: Server;
server: HttpServer | HttpsServer;
wss: websocket.Server;
/**
@@ -12,7 +13,7 @@ export default class WSSignaling {
* @param server HTTP服务器实例
* @param mode 通信模式public或private
*/
constructor(server: Server, mode: string) {
constructor(server: HttpServer | HttpsServer, mode: string) {
// 保存服务器实例
this.server = server;
// 创建WebSocket服务器