-
Notifications
You must be signed in to change notification settings - Fork 7
Chat
The chat module of K'tah! is a view component (chat.jade) that allows registered players to communicate with one another in and out of games. Chat is handled via one controller script (chat-controller.js) that routes messages between users. Each user makes periodic AJAX GET requests to /chat/:roomName which checks that user's "inbox." Whenever a player POSTs a message, that message is placed in the inbox of every other user associated with that room, where it is retrieved on the next AJAX request.
Room management, including constructing rooms and adding users to and removing users from rooms, is handled in response to requests. To join, on page load the client-side script (js/modules/chat/client.js) sends a POST request of type 'join' to /chat/:roomName, and the controller adds the user to the chat room and auto-messages the other users to this effect. If a user tries to join a room that does not yet exist, the controller creates that room and adds the user to it. On page unload, the client-side sends a POST request of type 'part'.
Upon joining, the client side receives a list of the other users in the chat room and builds a div of a written list of these users. As the client side receives notifications that others have joined or left, this list and its written form are kept up-to-date.
The chat controller features fallbacks when there is client-side malfunction.
- If the browser does not fire the unload event and send the request to part from the room (such as if the network connection is lost), the user will not be immediately parted from the room. To account for this, the controller keeps track of the last request from each user. If the user has not made a request in a 30-second period, the user is deleted from the room.
- If the client side tries to make a GET request if that user is not registered in that room (for example if the connection was lost for more than 30 seconds but re-established), the controller responds with a message which instructs the client to re-join that room.