Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store users in session #231

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Store users in session #231

wants to merge 5 commits into from

Commits on Sep 18, 2024

  1. Store session in socket.data

    This will allow us to fetch the session later by accessing the
    socket.data object in the server or client.
    We no longer autoconnect but only connect once a user has submitted
    their name. This prevents users being added to the session before they
    have comitted to the game. This allows users to retain their gamestate
    if their connection drops for any reason.
    This is following the guidance outlined here:
    https://socket.io/get-started/private-messaging-part-1/
    
    Co-authored-by: Ynda Jas <ynda@dxw.com>
    Co-authored-by: George Eaton <george@dxw.com>
    Co-authored-by: Liz Daly <liz.daly@dxw.com>
    3 people authored and rich committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    e06d65e View commit details
    Browse the repository at this point in the history
  2. Extract addPlayerHandler

    We want this logic in two places - when a player clicks join game
    (listening to the 'players:post') event and when restoring session of a
    previously joined player. It makes sense to abstract it to a function. I
    think it would be good if we extracted all event handlers like this to
    improve readability.
    rich committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    eef5d58 View commit details
    Browse the repository at this point in the history
  3. Add a session storage

    This will be used to store sessions serverside.
    This could be replaced by something like Redis or SQLite in future but
    this class is good enough for now.
    We save the session on connection and disconnection so it should always
    be up to date
    Gweaton authored and rich committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    0d1e018 View commit details
    Browse the repository at this point in the history
  4. Save the sessionId clientside

    We need a way for users to reconnect to their serverside session if
    their connection drops or they leave the page and return.
    We use sessionStorage so that the session is not shared across tabs as
    it would be for localStorage.
    We receive the "session:set" event from the server which tells us the
    server has save the session and now it's time for the client to do the
    same.
    We use the "DOMContentLoaded" event to attempt to read the sessionId
    from localStorage, if it exists then we connect. If it doesn't exist the
    user will have to manually connect by entering their name.
    
    Co-authored-by: Ynda Jas <ynda@dxw.com>
    Co-authored-by: George Eaton <george@dxw.com>
    Co-authored-by: Liz Daly <liz.daly@dxw.com>
    4 people committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    6b3c533 View commit details
    Browse the repository at this point in the history
  5. Replace socketId with sessionId

    Now we have a reliable way of tracking users we can use it across the
    app.
    
    Co-authored-by: Liz Daly <liz.daly@dxw.com>
    Co-authored-by: Rich James <rich.james@dxw.com>
    Co-authored-by: George Eaton <george@dxw.com>
    4 people committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    d4f38c7 View commit details
    Browse the repository at this point in the history