* fix: changed test text to use should * fix: corrected typo Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: corrected typo Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: remove unnecessary backslash Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: simplified text Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: added period Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>
2.5 KiB
2.5 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
589fc831f9fc0f352b528e76 | Handle a Disconnect | 2 | 301552 |
Description
To do this, add in to your existing connect listener a listener that listens for 'disconnect' on the socket with no data passed through. You can test this functionality by just logging to the console a user has disconnected.
socket.on('disconnect', () => { /*anything you want to do on disconnect*/ });
To make sure clients continuously have the updated count of current users, you should decrease the currentUsers by 1 when the disconnect happens then emit the 'user count' event with the updated count!
NoteJust like 'disconnect', all other events that a socket can emit to the server should be handled within the connecting listener where we have 'socket' defined. Submit your page when you think you've got it right.
Instructions
Tests
tests:
- text: Server should handle the event disconnect from a socket.
testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /socket.on.*('|")disconnect('|")/gi, ''); }, xhr => { throw new Error(xhr.statusText); })
- text: Your client should be listening for 'user count' event.
testString: getUserInput => $.get(getUserInput('url')+ '/public/client.js') .then(data => { assert.match(data, /socket.on.*('|")user count('|")/gi, 'Your client should be connection to server with the connection defined as socket'); }, xhr => { throw new Error(xhr.statusText); })
Challenge Seed
Solution
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/