Socket.io

  • For reference, this is the Multiplayer.js code
export class Socket{
/**
 * @property {boolean} shouldBeSynced - should we be connected to the server
 * @property {Object} socket - used by Multiplayer, creates socket for the client
 * @property {string} socketID - id given by the websocket server when a player connects
 */
// Sets up the AWS/Local Server
    static shouldBeSynced = true;
    static socket = io("wss://platformer.nighthawkcodingsociety.com"); //aws server
    //static socket = io(`ws://${window.location.host.split(":")[0]}:3000`); //local server
    static socketId;
    static {
        this.socket.on("id",(id)=>{this.socketId = id});
    }

    constructor(){throw new Error('Socket is a static class and cannot be instantiated.');}

    // socket.emit emits a message to the server
    static sendData(key,value) {
        if (this.shouldBeSynced == false){return "offline"};
        if (typeof key != "string"){return "key is not a string"};
        this.socket.emit(key,value);
    }
    // shouldBeSynced is a function to sync the players and the server
    static createListener(key, func){
        if (this.shouldBeSynced == false){return "offline"};
        if (typeof key != "string"){return "key is not a string"};
        this.socket.on(key,func);
    }
    // remove the socket / multiplayer mode
    static removeListener(key){
        if (typeof key == "string"){return "key is not a string"};
        this.socket.off(key)
    }

    static removeAllListeners(){
        this.socket.removeAllListeners();
    }

    static changeStatus(){
        this.shouldBeSynced = !this.shouldBeSynced;
        if(this.shouldBeSynced){
            this.removeAllListeners();

            GameControl.transitionToLevel(GameEnv.levels[GameEnv.levels.indexOf(GameEnv.currentLevel)]);
        } else{
            GameControl.transitionToLevel(GameEnv.levels[GameEnv.levels.indexOf(GameEnv.currentLevel)]);
        }
        return this.shouldBeSynced;
    }
}
export default Socket;
  • This creates a class for the Socket. A socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network.
  • Since I am in the Multiplayer team, Socket is basically the set-up for the multiplayer mode.
  • Keywords:
  • shouldBeSynced - should we be connected to the server
  • @property {Object} socket - used by Multiplayer, creates socket for the client
  • @property {string} socketID - id given by the websocket server when a player connects

Making the Enter Key Button

  • For reference, here’s the code for the enter key button. This is located in the Chat.js file.
var message = input.value;
                message = this.parseMessage(message);
                addMessage(message,"you");
                this.sendMessage(message);
            }
        button.addEventListener("click",onMessage.bind(this));

        function KeyCheck(e){
            console.log(this)
            if(e.key == "Enter"){
                onMessage.bind(this)()
            }
        }
        window.addEventListener("keypress",KeyCheck.bind(this));
  • For the chat function, the original way to send a message is to click the button, “Send”.
  • This code makes it so that pressing the “Enter” key also sends the message along with the ‘click’.
  • This is done by adding a function for the enter keycheck, which is simply “e”.
  • When the enter button is pressed for the chat, it is binded with the onMessage function to send the message.

Index / RPG (never mind -_-)

  • This is the mario sprite that shows up in the homepage. Originally, he moves only to the right and cannot go back.
  • Never mind

Non-Code Contributions:

  • Made the opening issue
  • Worked on AWS when necessary (e.g. rebooting the server, fixing multiplayer entirely)
  • Made pull requests
  • Communicated with the management team often (Ryan)
  • Spent a lot of effort in the class (it may not look like it because I wasn’t able to talk or explain code, but I tried my best). For example, I once worked with Trystan for 6 hours straight after school to fix the multiplayer code.