forked from colyseus/colyseus-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
51 lines (39 loc) · 1.63 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as path from 'path';
import * as express from 'express';
import * as serveIndex from 'serve-index';
import { createServer } from 'http';
import { Server } from 'colyseus';
import { monitor } from '@colyseus/monitor';
// Import demo room handlers
import { ChatRoom } from "./rooms/01-chat-room";
import { StateHandlerRoom } from "./rooms/02-state-handler";
import { AuthRoom } from "./rooms/03-auth";
import { CreateOrJoinRoom } from "./rooms/04-create-or-join-room";
const port = Number(process.env.PORT || 2567);
const app = express();
// Attach WebSocket Server on HTTP Server.
const gameServer = new Server({
server: createServer(app)
});
// Register ChatRoom as "chat"
gameServer.register("chat", ChatRoom);
// Register ChatRoom with initial options, as "chat_with_options"
// onInit(options) will receive client join options + options registered here.
gameServer.register("chat_with_options", ChatRoom, {
custom_options: "you can use me on Room#onInit"
});
// Register StateHandlerRoom as "state_handler"
gameServer.register("state_handler", StateHandlerRoom);
// Register StateHandlerRoom as "state_handler"
gameServer.register("auth", AuthRoom);
// Register CreateOrJoin as "create_or_join"
gameServer.register("create_or_join", CreateOrJoinRoom);
app.use('/', express.static(path.join(__dirname, "static")));
app.use('/', serveIndex(path.join(__dirname, "static"), {'icons': true}))
// (optional) attach web monitoring panel
app.use('/colyseus', monitor(gameServer));
gameServer.onShutdown(function(){
console.log(`game server is going down.`);
});
gameServer.listen(port);
console.log(`Listening on http://localhost:${ port }`);