// chat.js
// chat test run
module.exports = function(io) {
	var express = require('express');
	var router = express.Router();

	router.get('/', function(req, res) {
		var path = require('path');
		res.sendFile(path.resolve('public/frontpage.html'));
	});

    io.on('connection', function(socket) {
    	
    	//new user joined
    	socket.on('add user', function(username) {
			// we store the username in the socket session for this client
    		socket.user = username;
    		var message = username + ' has joined the conversation.';
    		socket.broadcast.emit('new message', {
    			event: 'connected',
    			user: socket.user
    		});
    	});

    	//automatically disconnects user
    	socket.on('disconnect', function(){
    		console.log(socket.user + " has left.");
    		var message = socket.user + 'has left the conversation'
    		socket.broadcast.emit('new message', {
    			event: 'disconnected',
    			user: socket.user
    		});
    	});

    	//new message sent
		socket.on('new message', function (data) {
		// we tell the client to execute 'new message'
			socket.broadcast.emit('new message', {
			  text: data.text,
			  user: data.user
			});
			console.log(data);
		});
    });

    return router;
};

@@ -119,16 +119,68 @@
 outer();
+%0A    var accounts = require('../database/accounts');
 %0A%0A%09route
@@ -349,14 +349,8 @@
 ) %7B%0A
-    %09%0A
     
@@ -404,24 +404,16 @@
 unction(
-username
 ) %7B%0A%09%09%09/
@@ -476,24 +476,27 @@
 lient%0A    %09%09
+// 
 socket.user 
@@ -505,32 +505,35 @@
 username;%0A    %09%09
+// 
 var message = us
@@ -576,24 +576,27 @@
 on.';%0A    %09%09
+// 
 socket.broad
@@ -620,32 +620,35 @@
 ssage', %7B%0A    %09%09
+// 
 %09event: 'connect
@@ -650,32 +650,35 @@
 nnected',%0A    %09%09
+// 
 %09user: socket.us
@@ -678,36 +678,153 @@
 cket.user%0A    %09%09
+// 
 %7D);%0A
+            var username = %22User %22 + getRandomInt(0, 1000);%0A            socket.emit('assign username', username);%0A
     %09%7D);%0A%0A    %09/
@@ -1394,8 +1394,106 @@
 ter;%0A%7D;%0A
+%0Afunction getRandomInt(min, max) %7B%0A    return Math.floor(Math.random() * (max - min + 1)) + min;%0A%7D

generate random number for user