From 3321e7f3cd52bd229377f456019e1c34ce80aaa9 Mon Sep 17 00:00:00 2001 From: Rajiv Ram V Date: Thu, 7 Apr 2016 10:40:14 +0530 Subject: [PATCH] Updated README, added test framework --- README.md | 2 +- chat-server.js | 6 ++--- test/load-test.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 test/load-test.js diff --git a/README.md b/README.md index c9b217a..daded41 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,4 @@ A simple Node.js chat application 4. You can now connect to the server using the command `nc 3000`. ## Client -1. Instead of netcat, you can connect to the server using the chatter-box client using the command `node chat-client.js` +1. Instead of netcat, you can connect to the server using the chatter-box client using the command `node chat-client.js`. diff --git a/chat-server.js b/chat-server.js index e3367bc..8a5e731 100644 --- a/chat-server.js +++ b/chat-server.js @@ -80,9 +80,9 @@ function broadcastUserJoining(newUserNickName){ } function sendUserNickNameList(){ - var nickNames = [], - keysIter = users.keys(), - i = 0; + var nickNames = [], + keysIter = users.keys(), + i = 0; for (;i 0; i--) + + // Connect to the server + var client = net.connect(...); + + // Usual stuff, *once* the client connects, handle the initial handshake and login protocol + client.once('connect', handleChatLoginProtocol); + + // I have just chosen this namespaced (generally important) custom event to notify completion of protocol + // Also note that this 'subscriber pattern' allows me to avoid complex code to call done() once ALL the clients have connected + client.once('GQMe-login-protocol-completed', handleLogin); + + // You can use an array instead of a Map object. + clients.set(i, client); + + }); + + // Remember that you need to call done() only after we know for certain that all the clients have completed their login protocol. + function handleLogin(){ + if (++connectedClients === parallelConnections) done(); + } + + // When you reach here, you should be having clients variable filled with logged in clients. Go ahead and have fun! + // I have added a few test cases to help you get started + it('should be able to send messages from many clients to a single client'); + it('should be able to send a message to a client when that client is sending a message'); + +}) + +function handleChatLoginProtocol(){ + + // Note that the keyword 'this' in this function when evoked as a 'connect' event handler + // will refer to the socket which is a stream as well as an event emitter. Hence you can do this.write(..), this.on(...), this.once(...), this.emit(...) etc. + // to help with handling the protcol. + + // IMPORTANT: Once you complete handling the handshake and login, remember to emit 'GQMe-login-protocol-completed' + + ... + +} \ No newline at end of file