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
52
53
54
55
56
57
58
59
60
|
import { Game } from "../../engine/Game";
import { Floor, Player } from "../../engine/entities";
import {
WallBounds,
Physics,
Collision,
MessageQueueProvider,
MessagePublisher,
} from "../../engine/systems";
import { Miscellaneous } from "../../engine/config";
const TICK_RATE = 60 / 1000;
class Server {
private server: any;
private game: Game;
constructor() {
this.game = new Game();
[
new Physics(),
new Collision({
width: Miscellaneous.WIDTH,
height: Miscellaneous.HEIGHT,
}),
new WallBounds(Miscellaneous.WIDTH),
].forEach((system) => this.game.addSystem(system));
[new Floor(160), new Player()].forEach((entity) =>
this.game.addEntity(entity),
);
this.game.start();
setInterval(() => {
this.game.doGameLoop(performance.now());
}, TICK_RATE);
this.server = Bun.serve<any>({
websocket: {
open(ws) {
ws.subscribe("the-group-chat");
ws.publish("the-group-chat", msg);
},
message(ws, message) {
// this is a group chat
// so the server re-broadcasts incoming message to everyone
ws.publish("the-group-chat", `${ws.data.username}: ${message}`);
},
close(ws) {
const msg = `${ws.data.username} has left the chat`;
ws.unsubscribe("the-group-chat");
ws.publish("the-group-chat", msg);
},
},
});
}
}
new Server();
|