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