Can recieve messages, and update clients about it.
This commit is contained in:
parent
ca6d822d7c
commit
7039776008
42
main.js
42
main.js
|
@ -101,7 +101,7 @@ getrows_sqlite3("chats", function(data)
|
||||||
getrows_sqlite3("messages", function(data)
|
getrows_sqlite3("messages", function(data)
|
||||||
{
|
{
|
||||||
// Push all the messages to the chats
|
// 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.");
|
console.log("Server ready.");
|
||||||
|
@ -452,6 +452,9 @@ var server = net.createServer(function(socket)
|
||||||
// Is the user now logged in
|
// Is the user now logged in
|
||||||
if(sock.logged_in)
|
if(sock.logged_in)
|
||||||
{
|
{
|
||||||
|
// Set the users username
|
||||||
|
sock.username = data.username;
|
||||||
|
|
||||||
// Add the user to the connections array
|
// Add the user to the connections array
|
||||||
sock.connection_id = connections.length;
|
sock.connection_id = connections.length;
|
||||||
connections.push(sock);
|
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<connections.length;i++)
|
||||||
|
{
|
||||||
|
// Is the client connected
|
||||||
|
if(connections[i])
|
||||||
|
{
|
||||||
|
// Send the client the message
|
||||||
|
socket_write(connections[i], {
|
||||||
|
mode: "new_message",
|
||||||
|
message: data.message,
|
||||||
|
channel: data.channel,
|
||||||
|
from: sock.username
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue