From df3970d8b097d9e0f42fa107e74849a296c1b723 Mon Sep 17 00:00:00 2001 From: JianMiau Date: Mon, 11 Sep 2023 15:58:59 +0800 Subject: [PATCH] =?UTF-8?q?[add]=20=E7=92=B0=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++- src/index.ts | 29 ++++++++++++++++++++++++----- yarn.lock | 5 +++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 2305205..0f9b05e 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "dependencies": { "dayjs": "^1.11.9", "dotenv": "^16.3.1", + "fs": "^0.0.1-security", "tsrpc": "^3.4.12" } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index 3980fe4..be88a2d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import dayjs from "dayjs"; import "dayjs/locale/zh-tw"; import dotenv from "dotenv"; +import fs from "fs"; import * as path from "path"; import { BaseConnection, WsServer } from "tsrpc"; import Lobby from "./component/Lobby/Lobby"; @@ -15,13 +16,31 @@ BaseEnumerator.Init(); dayjs.locale("zh-tw"); dotenv.config(); +const prikey: string = fs.readFileSync("./certificate/RSA-privkey.pem", "utf8"); +const cert: string = fs.readFileSync("./certificate/RSA-cert.pem", "utf8"); + // Create the Server const port: number = +process.env.PORT || 3003; -export const server: WsServer = new WsServer(serviceProto, { - port: port, - // Remove this to use binary mode (remove from the client too) - json: true -}); +export let server: WsServer = undefined; +console.log(`NODE_ENV ${process.env.NODE_ENV}`); +if (process.env.NODE_ENV === "prod") { + server = new WsServer(serviceProto, { + port: port, + // Remove this to use binary mode (remove from the client too) + json: true, + // 启用 WSS + wss: { + key: prikey, + cert: cert + }, + }); +} else { + server = new WsServer(serviceProto, { + port: port, + // Remove this to use binary mode (remove from the client too) + json: true + }); +} // Initialize before server start async function init() { diff --git a/yarn.lock b/yarn.lock index 6034343..a66f897 100644 --- a/yarn.lock +++ b/yarn.lock @@ -795,6 +795,11 @@ fs.realpath@^1.0.0: resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== +fs@^0.0.1-security: + version "0.0.1-security" + resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4" + integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w== + fsevents@~2.3.2: version "2.3.3" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"