diff --git a/main.js b/main.js index 9de40bb..67ad3f6 100644 --- a/main.js +++ b/main.js @@ -101,7 +101,7 @@ getrows_sqlite3("chats", function(data) getrows_sqlite3("messages", function(data) { // Push all the messages to the chats - chats[data.chat].messages.push(data.message); + chats[data.channel].messages.push(data.message); }); console.log("Server ready."); @@ -452,6 +452,9 @@ var server = net.createServer(function(socket) // Is the user now logged in if(sock.logged_in) { + // Set the users username + sock.username = data.username; + // Add the user to the connections array sock.connection_id = connections.length; connections.push(sock); @@ -544,6 +547,43 @@ var server = net.createServer(function(socket) } } } + + // Does the user want to send a message + if(data.mode == "send_message") + { + // Is the channel real + if(chats[data.channel]) + { + // Save the message to sqlite3 + sqlite3_insertData("messages", { + channel: data.channel, + message: data.message, + from: sock.username + }); + + // Add it to the channel + chats[data.channel].messages.push({ + message: data.message, + from: sock.username + }); + + // Send it to all the connected clients + for(var i=0;i