aboutsummaryrefslogtreecommitdiffstats
path: root/src/server
diff options
context:
space:
mode:
authorRblSb <msrblsb@gmail.com>2020-02-22 06:35:57 +0300
committerRblSb <msrblsb@gmail.com>2020-02-22 06:35:57 +0300
commit08d068181045f91f0aa160f9b088a18048b948ec (patch)
treea995230c279a5a1fd849dc5202301d15474bca61 /src/server
parent4e0859a0f902e84cfafb38103e0be9f5b81d7abe (diff)
More client buttons
Diffstat (limited to 'src/server')
-rw-r--r--src/server/Main.hx37
1 files changed, 31 insertions, 6 deletions
diff --git a/src/server/Main.hx b/src/server/Main.hx
index bc52718..45be2af 100644
--- a/src/server/Main.hx
+++ b/src/server/Main.hx
@@ -21,6 +21,7 @@ class Main {
final wss:WSServer;
final config:Config;
final clients:Array<Client> = [];
+ final freeIds:Array<Int> = [];
final videoList:Array<VideoItem> = [];
final videoTimer = new VideoTimer();
final messages:Array<Message> = [];
@@ -72,8 +73,12 @@ class Main {
function onConnect(ws:WebSocket, req):Void {
final ip = req.connection.remoteAddress;
- trace('Client connected ($ip)');
- final client = new Client(ws, "Unknown", false);
+ final id = freeIds.length > 0 ? freeIds.shift() : clients.length;
+ final name = 'Guest ${id + 1}';
+ trace('$name connected ($ip)');
+ final isAdmin = req.connection.localAddress == ip;
+ final client = new Client(ws, id, name, 0);
+ if (isAdmin) client.group.set(Admin);
clients.push(client);
if (clients.length == 1)
if (videoTimer.isPaused()) videoTimer.play();
@@ -98,6 +103,7 @@ class Main {
});
ws.on("close", err -> {
trace('Client ${client.name} disconnected');
+ sortedPush(freeIds, client.id);
clients.remove(client);
sendClientList();
if (client.isLeader) {
@@ -107,6 +113,17 @@ class Main {
});
}
+ function sortedPush(ids:Array<Int>, id:Int):Void {
+ for (i in 0...ids.length) {
+ final n = ids[i];
+ if (id < n) {
+ ids.insert(i, id);
+ return;
+ }
+ }
+ ids.push(id);
+ }
+
function onMessage(client:Client, data:WsEvent):Void {
switch (data.type) {
case Connected:
@@ -131,11 +148,13 @@ class Main {
case LoginError:
case Logout:
final oldName = client.name;
- client.name = "Unknown";
+ final id = clients.indexOf(client) + 1;
+ client.name = 'Guest $id';
send(client, {
type: data.type,
logout: {
- clientName: oldName,
+ oldClientName: oldName,
+ clientName: client.name,
clients: clientList()
}
});
@@ -150,7 +169,7 @@ class Main {
data.message.clientName = client.name;
final time = "[" + new Date().toTimeString().split(" ")[0] + "] ";
messages.push({text: text, name: client.name, time: time});
- if (messages.length > config.serverChatHistory) messages.pop();
+ if (messages.length > config.serverChatHistory) messages.shift();
broadcast(data);
case AddVideo:
videoList.push(data.addVideo.item);
@@ -202,7 +221,11 @@ class Main {
broadcastExcept(client, data);
case SetLeader:
clients.setLeader(data.setLeader.clientName);
- sendClientList();
+ broadcast({
+ type: SetLeader, setLeader: {
+ clientName: data.setLeader.clientName
+ }
+ });
if (videoList.length == 0) return;
if (!clients.hasLeader()) {
if (videoTimer.isPaused()) videoTimer.play();
@@ -212,6 +235,8 @@ class Main {
}
});
}
+ case ClearChat:
+ if (client.isAdmin) broadcast(data);
}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage