/* jshint node: true */

var io = require('socket.io');

var chatServer = (function (io) {
  var connections = [];
  var conversation = [];

  var server = io.listen(1337);
  /*
   * level of detail output to logger
   * 0 - error
   * 1 - warn
   * 2 - info
   * 3 - debug (default)
   */
  server.set('log level', 2);
  server.sockets.on('connection', function (socket) {
    connections.push(socket);
    socket.on('setNickname', function (name) {
      socket.set('nickname', name);
      socket.emit('initConversation', conversation);
      socket.emit('updateUserList', listUsers());
      socket.broadcast.emit('updateUserList', listUsers());
    });
    socket.on('newMessage', function (data) {
      socket.get('nickname', function (err, name) {
        var message = {user: name, message: data.message};
        conversation.push(message);
        /* TODO: do I really need to do this twice? */
        socket.emit('pushMessage', message);
        socket.broadcast.emit('pushMessage', message);
      });
    });
  });

  function listUsers() {
    var userList = [];
    for (var i = 0, len = connections.length; i < len; i++) {
      if (connections[i].store.data.nickname) {
        userList.push(connections[i].store.data.nickname);
      }
    }
    return userList;
  }
  return server;
}(io));

console.log('Chat is go...');

@@ -130,16 +130,962 @@
 tion = %5B
+%0A    // dummy conversation already in progress...%0A    %7Buser: 'mike', message: 'this is mike here'%7D,%0A    %7Buser: 'jeff', message: 'hey mike, it%5C's jeff'%7D,%0A    %7Buser: 'mike', message: 'oh cool'%7D,%0A    %7Buser: 'jeff', message: 'yeah kind of'%7D,%0A    %7Buser: 'steve', message: 'now im steve i guess'%7D,%0A    %7Buser: 'earl', message: 'and im earl'%7D,%0A    %7Buser: 'josef', message: 'roly-poly'%7D,%0A    %7Buser: 'jose', message: 'really'%7D,%0A    %7Buser: 'gertie', message: 'weee'%7D,%0A    %7Buser: 'allison', message: 'boo'%7D,%0A    %7Buser: 'ellie', message: 'so cool'%7D,%0A    %7Buser: 'luis', message: 'yo amo'%7D,%0A    %7Buser: 'roberto', message: 'we will walk the paved path'%7D,%0A    %7Buser: 'sylvie', message: 'yoiks'%7D,%0A    %7Buser: 'sylvie', message: 'oh noes'%7D,%0A    %7Buser: 'sylvie', message: 'c%5C'mon'%7D,%0A    %7Buser: 'sylvie', message: 'jsut jake'%7D,%0A    %7Buser: 'matt', message: 'what%5C's doing?'%7D,%0A    %7Buser: 'matt', message: 'nothing'%7D,%0A    %7Buser: 'lebron', message: 'what happens now?'%7D%0A  
 %5D;%0A%0A  va

Add some dummy conversation to start