summaryrefslogtreecommitdiff
path: root/server/src/server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/server.ts')
-rw-r--r--server/src/server.ts78
1 files changed, 33 insertions, 45 deletions
diff --git a/server/src/server.ts b/server/src/server.ts
index 9a73f11..d169f7d 100644
--- a/server/src/server.ts
+++ b/server/src/server.ts
@@ -11,50 +11,38 @@ 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);
- },
+const game = new Game();
+
+[new Physics(), new Collision(), new WallBounds(Miscellaneous.WIDTH)].forEach(
+ (system) => game.addSystem(system),
+);
+
+[new Floor(160), new Player()].forEach((entity) => game.addEntity(entity));
+
+game.start();
+
+setInterval(() => {
+ game.doGameLoop(performance.now());
+}, TICK_RATE);
+
+const server = Bun.serve({
+ port: 8080,
+ fetch(req, server) {
+ const sessionId = Math.floor(Math.random() * 1e10).toString();
+
+ server.upgrade(req, {
+ headers: {
+ "Set-Cookie": `SessionId=${sessionId}`,
},
});
- }
-}
-
-new Server();
+ },
+ websocket: {
+ open(ws) {},
+ message(ws, message) {
+ console.log(message);
+ },
+ close(ws) {},
+ },
+});
+
+console.log(`Listening on ${server.hostname}:${server.port}`);