-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: Implement server side of long polling.
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
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |