添加全局的isProd参数
This commit is contained in:
parent
68cebfdb7b
commit
3bdfeaec64
1
src/global.d.ts
vendored
1
src/global.d.ts
vendored
@ -18,6 +18,7 @@ declare global {
|
|||||||
NODE_ENV: string
|
NODE_ENV: string
|
||||||
ROOT_PATH: string
|
ROOT_PATH: string
|
||||||
$cfg: any
|
$cfg: any
|
||||||
|
isProd: boolean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
67
src/index.ts
67
src/index.ts
@ -1,55 +1,56 @@
|
|||||||
import http from "http";
|
import http from 'http'
|
||||||
import express from "express";
|
import express from 'express'
|
||||||
import cors from "cors";
|
import cors from 'cors'
|
||||||
import { RedisPresence, Server } from 'colyseus'
|
import { RedisPresence, Server } from 'colyseus'
|
||||||
import {monitor} from "@colyseus/monitor";
|
import { monitor } from '@colyseus/monitor'
|
||||||
import rateLimit from "express-rate-limit";
|
import rateLimit from 'express-rate-limit'
|
||||||
// import socialRoutes from "@colyseus/social/express"
|
// import socialRoutes from "@colyseus/social/express"
|
||||||
import {GeneralRoom} from "./rooms/GeneralRoom";
|
import { GeneralRoom } from './rooms/GeneralRoom'
|
||||||
import {initData} from "./common/GConfig";
|
import { initData } from './common/GConfig'
|
||||||
import {Config} from "./cfg/Config";
|
import { Config } from './cfg/Config'
|
||||||
import {RankedLobbyRoom} from "./rooms/RankedLobbyRoom";
|
import { RankedLobbyRoom } from './rooms/RankedLobbyRoom'
|
||||||
import { MongooseDriver } from 'colyseus/lib/matchmaker/drivers/MongooseDriver'
|
import { MongooseDriver } from 'colyseus/lib/matchmaker/drivers/MongooseDriver'
|
||||||
import { Service } from './service/Service'
|
import { Service } from './service/Service'
|
||||||
import { RedisClient } from './redis/RedisClient'
|
import { RedisClient } from './redis/RedisClient'
|
||||||
|
|
||||||
require('./rooms/MSender');
|
require('./rooms/MSender')
|
||||||
require('./rooms/RoomExtMethod');
|
require('./rooms/RoomExtMethod')
|
||||||
require('./common/Extend');
|
require('./common/Extend')
|
||||||
|
|
||||||
const isProd = process.env.NODE_ENV === 'production'
|
const isProd = process.env.NODE_ENV === 'production'
|
||||||
|
|
||||||
let config: Config = require('../config/config.json');
|
let config: Config = require('../config/config.json')
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors())
|
||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
initData();
|
initData()
|
||||||
const server = http.createServer(app);
|
global.isProd = isProd
|
||||||
|
const server = http.createServer(app)
|
||||||
let port: number
|
let port: number
|
||||||
let gameServer: Server
|
let gameServer: Server
|
||||||
if (isProd) {
|
if (isProd) {
|
||||||
port = Number(process.env.PORT) + Number(process.env.NODE_APP_INSTANCE);
|
port = Number(process.env.PORT) + Number(process.env.NODE_APP_INSTANCE)
|
||||||
gameServer = new Server({
|
gameServer = new Server({
|
||||||
server,
|
server,
|
||||||
driver: new MongooseDriver(config.mongodb),
|
driver: new MongooseDriver(config.mongodb),
|
||||||
presence: new RedisPresence({url: config.redis}),
|
presence: new RedisPresence({ url: config.redis })
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
port = Number(process.env.PORT || 2567);
|
port = Number(process.env.PORT || 2567)
|
||||||
gameServer = new Server({
|
gameServer = new Server({
|
||||||
server,
|
server
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// register your room handlers
|
// register your room handlers
|
||||||
gameServer.define('general_room', GeneralRoom);
|
gameServer.define('general_room', GeneralRoom)
|
||||||
gameServer
|
gameServer
|
||||||
.define('match_room', RankedLobbyRoom)
|
.define('match_room', RankedLobbyRoom)
|
||||||
.filterBy(['numClientsToMatch']);
|
.filterBy(['numClientsToMatch'])
|
||||||
/**
|
/**
|
||||||
* Register @colyseus/social routes
|
* Register @colyseus/social routes
|
||||||
*
|
*
|
||||||
@ -59,31 +60,31 @@ gameServer
|
|||||||
// app.use("/", socialRoutes);
|
// app.use("/", socialRoutes);
|
||||||
|
|
||||||
// register colyseus monitor AFTER registering your room handlers
|
// register colyseus monitor AFTER registering your room handlers
|
||||||
app.use("/colyseus", monitor());
|
app.use('/colyseus', monitor())
|
||||||
|
|
||||||
// 限制每2分钟最多连接 max次, 防止恶意的创建空房间
|
// 限制每2分钟最多连接 max次, 防止恶意的创建空房间
|
||||||
const apiLimiter = rateLimit({
|
const apiLimiter = rateLimit({
|
||||||
windowMs: 2 * 60 * 1000, // 2 minutes
|
windowMs: 2 * 60 * 1000, // 2 minutes
|
||||||
max: 20
|
max: 20
|
||||||
});
|
})
|
||||||
app.use("/matchmake/", apiLimiter);
|
app.use('/matchmake/', apiLimiter)
|
||||||
|
|
||||||
// 设置反向代理后, 须设置该值
|
// 设置反向代理后, 须设置该值
|
||||||
// see https://expressjs.com/en/guide/behind-proxies.html
|
// see https://expressjs.com/en/guide/behind-proxies.html
|
||||||
app.set('trust proxy', 1);
|
app.set('trust proxy', 1)
|
||||||
|
|
||||||
let opts = {url: config.redis}
|
let opts = { url: config.redis }
|
||||||
new RedisClient(opts)
|
new RedisClient(opts)
|
||||||
const services = new Service()
|
const services = new Service()
|
||||||
gameServer.onShutdown(function () {
|
gameServer.onShutdown(function () {
|
||||||
console.log("master process is being shut down!");
|
console.log('master process is being shut down!')
|
||||||
//TODO:: 保存所有数据至db, 重启时恢复
|
//TODO:: 保存所有数据至db, 重启时恢复
|
||||||
});
|
})
|
||||||
services.registSelf(port)
|
services.registSelf(port)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return gameServer.listen(port)
|
return gameServer.listen(port)
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
console.log(`Listening on ws://localhost:${port}`)
|
console.log(`Listening on ws://localhost:${ port }`)
|
||||||
});
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ const port = Number(process.env.PORT || 2500);
|
|||||||
|
|
||||||
let config: Config = require('../config/config.json');
|
let config: Config = require('../config/config.json');
|
||||||
const isProd = process.env.NODE_ENV === 'production'
|
const isProd = process.env.NODE_ENV === 'production'
|
||||||
|
global.isProd = isProd
|
||||||
initData();
|
initData();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user