Made the server update users about new users
This commit is contained in:
parent
87207647e0
commit
ed526ff3c4
45
main.js
45
main.js
|
@ -380,6 +380,9 @@ function socket_init(socket, ondata)
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a connections varible
|
||||||
|
var connections = [];
|
||||||
|
|
||||||
// Create a server
|
// Create a server
|
||||||
var server = net.createServer(function(socket)
|
var server = net.createServer(function(socket)
|
||||||
{
|
{
|
||||||
|
@ -390,11 +393,8 @@ var server = net.createServer(function(socket)
|
||||||
sock = socket_init(socket, function(data)
|
sock = socket_init(socket, function(data)
|
||||||
{
|
{
|
||||||
// Does the user want to login
|
// Does the user want to login
|
||||||
if(data.mode == "login")
|
if(data.mode == "login" && !sock.logged_in)
|
||||||
{
|
{
|
||||||
// Set the logged in varible
|
|
||||||
var logged_in = false;
|
|
||||||
|
|
||||||
// Is the user registered here
|
// Is the user registered here
|
||||||
if(users[data.username])
|
if(users[data.username])
|
||||||
{
|
{
|
||||||
|
@ -407,7 +407,7 @@ var server = net.createServer(function(socket)
|
||||||
console.log("Logged "+sock.username+" in.");
|
console.log("Logged "+sock.username+" in.");
|
||||||
|
|
||||||
// Set logged in
|
// Set logged in
|
||||||
logged_in = true;
|
sock.logged_in = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -424,15 +424,43 @@ var server = net.createServer(function(socket)
|
||||||
password: data.password
|
password: data.password
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Send the status to all connections
|
||||||
|
for(var i=0;i<connections.length;i++)
|
||||||
|
{
|
||||||
|
// Does the connection exist
|
||||||
|
if(connections[i])
|
||||||
|
{
|
||||||
|
// Send data to the connection
|
||||||
|
socket_write(connections[i], {
|
||||||
|
mode: "new_user",
|
||||||
|
user: data.username,
|
||||||
|
data: {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Creating account "+data.username+".");
|
console.log("Creating account "+data.username+".");
|
||||||
|
|
||||||
// Set logged in
|
// Set logged in
|
||||||
logged_in = true;
|
sock.logged_in = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is the user now logged in
|
// Is the user now logged in
|
||||||
if(logged_in)
|
if(sock.logged_in)
|
||||||
{
|
{
|
||||||
|
// Add the user to the connections array
|
||||||
|
sock.connection_id = connections.length;
|
||||||
|
connections.push(sock);
|
||||||
|
|
||||||
|
// Set on disconnect
|
||||||
|
sock.sock.on("disconnect", function()
|
||||||
|
{
|
||||||
|
// Delete the connection id
|
||||||
|
delete connections[sock.connection_id];
|
||||||
|
});
|
||||||
|
|
||||||
// Get a list of chat keys
|
// Get a list of chat keys
|
||||||
var chat_keys = Object.keys(chats);
|
var chat_keys = Object.keys(chats);
|
||||||
var chats_client = {};
|
var chats_client = {};
|
||||||
|
@ -481,6 +509,9 @@ var server = net.createServer(function(socket)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Setup some varibles
|
||||||
|
sock.logged_in = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Listen for data
|
// Listen for data
|
||||||
|
|
Loading…
Reference in New Issue