@@ -36,9 +36,21 @@ export class GameServer<Config extends object = object> {
36
36
private readonly privateGames : Games < Config > = { } ;
37
37
public readonly logger = new Logger < Config > ( ) ;
38
38
39
+ /**
40
+ * Creates a new `GameServer`.
41
+ * @param info Information about the game server.
42
+ * @param cgePath The file path to the Code Game Events file about the game.
43
+ * @param webRoot The path to the root of the frontend. Specify `null` if there are no static assets.
44
+ * @param createGame A function that is called to create a new game. An instance of `Game` must be returned.
45
+ * @param port The port that the server will bind to.
46
+ * @param maxGamesCount The maximum number of games allowed on the server at once.
47
+ * @param heartbeatIntervalSeconds The time in seconds to wait before checking again whether a socket is still alive.
48
+ * @returns an instance of `GameServer`.
49
+ */
39
50
public constructor (
40
51
info : Info ,
41
52
cgePath : string ,
53
+ webroot : string | null ,
42
54
createGame : CreateGameFn < Config > ,
43
55
port : number = DEFAULT_PORT ,
44
56
maxGamesCount : number = DEFAULT_GAMES_COUNT ,
@@ -54,7 +66,7 @@ export class GameServer<Config extends object = object> {
54
66
// Api
55
67
const app = express ( ) ;
56
68
app . use ( cors ( ) ) ;
57
- app . use ( createApi ( this , cgePath , Object . assign ( info , { cg_version : CG_VERSION . join ( "." ) } ) ) ) ;
69
+ app . use ( createApi ( this , webroot , cgePath , Object . assign ( info , { cg_version : CG_VERSION . join ( "." ) } ) ) ) ;
58
70
59
71
// Game
60
72
this . createGameFn = createGame ;
@@ -70,6 +82,22 @@ export class GameServer<Config extends object = object> {
70
82
this . HEARTBEAT_INTERVAL_SECONDS = heartbeatIntervalSeconds ;
71
83
}
72
84
85
+ /**
86
+ * Stops the server from accepting new connections and keeps
87
+ * existing connections. This function is asynchronous, the
88
+ * server is finally closed when all connections are ended
89
+ * and the server emits a 'close' event. The optional callback
90
+ * will be called once the 'close' event occurs. Unlike that
91
+ * event, it will be called with an Error as its only argument
92
+ * if the server was not open when it was closed.
93
+ *
94
+ * See `http.Server.close` for more.
95
+ */
96
+ public close ( onCloseCallback ?: ( ( err ?: Error | undefined ) => void ) | undefined ) {
97
+ this . server . close ( onCloseCallback ) ;
98
+ // TODO: Close all WebSockets if they are not already closed.
99
+ }
100
+
73
101
/**
74
102
* Looks for a game in the `publicGames`, as well as in the `privateGames`.
75
103
* @param gameId The game ID.
0 commit comments