diff --git a/Queue.js b/Queue.js new file mode 100644 index 000000000..b1d6bfdcf --- /dev/null +++ b/Queue.js @@ -0,0 +1,11 @@ +var Queue = function(limit) { + this.limit = limit; + this.elements = []; +} +Queue.prototype.push = function(elem) { + this.elements.push(elem); + while (this.elements.length > this.limit) { + this.elements.shift(); + } +} +module.exports = Queue; \ No newline at end of file diff --git a/README.md b/README.md index c18a098c0..b454ec3b7 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,23 @@ the [Getting Started](http://socket.io/get-started/chat/) guide of the Socket.IO website. Please refer to it to learn how to run this application. + +# Homework + +Here are some ideas to improve the application: + + - Broadcast a message to connected users when someone connects or disconnects + - Add support for nicknames + - Don’t send the same message to the user that sent it himself. Instead, append the message directly as soon as he presses enter. + - Add “{user} is typing” functionality + - Show who’s online + - Add private messaging + - Share your improvements! + + - __(DONE)__ Joining users see up to a 100 of latest messages in their personal message boards + - __(DONE)__ The message board clearly indicates the authorship of messages by the same user + - __(DONE)__ The anonymous user identity is assigned automatically, and it survives browser restarts + +# Issues + + - Client: handle server disconnect, do not loose outgoing messages diff --git a/index.html b/index.html index 5f2767e98..9d3b4485b 100644 --- a/index.html +++ b/index.html @@ -7,8 +7,8 @@ body { font: 13px Helvetica, Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } - form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } - #messages { list-style-type: none; margin: 0; padding: 0; } + form button, form label { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } + #messages { list-style-type: none; margin: 0; padding: 0; padding-bottom: 100px; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } @@ -16,19 +16,54 @@
- +
+ diff --git a/index.js b/index.js index a322dca0f..bdf925c98 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,37 @@ var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); +var Queue = require('./Queue.js'); -app.get('/', function(req, res){ - res.sendFile(__dirname + '/index.html'); +var queue = new Queue(100); + +app.get('/', function (req, res) { + res.sendFile(__dirname + '/index.html'); }); -io.on('connection', function(socket){ - socket.on('chat message', function(msg){ - io.emit('chat message', msg); - }); +io.on('connection', function (socket) { + console.log('a user connected'); + if (queue.elements.length === 0) { + socket.emit('restoreServer', []); + } + socket.on('restoreClient', function () { + socket.emit('restoreClient', queue.elements); + }); + socket.on('restoreServer', function (messages) { + if (queue.elements.length === 0) { + queue.elements = messages; + } + }); + socket.on('chat message', function (msg) { + console.log('message: ', msg); + queue.push(msg); + io.emit('chat message', msg); + }); + socket.on('disconnect', function () { + console.log('user disconnected'); + }); }); -http.listen(3000, function(){ - console.log('listening on *:3000'); +http.listen(3000, function () { + console.log('listening on *:3000'); }); diff --git a/package.json b/package.json index 46f75814b..e88f47fc9 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.1", "description": "my first socket.io app", "dependencies": { - "express": "4.10.2", - "socket.io": "1.2.0" + "express": "^4.10.2", + "socket.io": "^1.3.3" } }