Skip to content

Commit

Permalink
api: Implement server side of long polling.
Browse files Browse the repository at this point in the history
This commit adds a handler for GET /events which
stores the response object for the client for emitting
events in the future.
  • Loading branch information
kuv2707 committed Jun 1, 2024
1 parent 57413d6 commit 09b7759
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backend/eventRecipients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// this module is responsible for handling the clients currently connected to the server.
// It stores the clients in a Map object, where the key is the client's user_id and the value is the client's http response object.

import { Response } from 'express';

const clients = new Map<string, Response>();

export function addClient(userId: string, res: Response) {
clients.set(userId, res);
}

export function removeClient(userId: string) {
clients.delete(userId);
}

export function getClient(userId: string) {
return clients.get(userId);
}
10 changes: 10 additions & 0 deletions backend/routes/gameRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from 'express';
import { addClient } from '../eventRecipients';
const router = express.Router();

router.get('/events', (req, res) => {
addClient('user_id', res);
});

// the post handler should retrieve the game the user is currently in, and update the game state.
// The request body contains the event data, as described in ARCHITECTURE.md

0 comments on commit 09b7759

Please sign in to comment.