cap the amount of pings users are allowed to send per second
This commit is contained in:
parent
98776062ca
commit
7014be3e25
2 changed files with 25 additions and 7 deletions
|
@ -54,7 +54,8 @@ server.on("connection", (socket) => {
|
|||
return;
|
||||
}
|
||||
const username = reader.readShortString();
|
||||
let page = reader.readString().toLowerCase().replace(".htm", "").replace(".html", "");
|
||||
const rawURL = reader.readString();
|
||||
let page = rawURL.toLowerCase().replace(".htm", "").replace(".html", "");
|
||||
if (page === "index") {
|
||||
page = "";
|
||||
}
|
||||
|
@ -70,7 +71,7 @@ server.on("connection", (socket) => {
|
|||
for (const otherUser of usersOnPage) {
|
||||
usersToSend.writeUInt(otherUser.id).writeShortString(otherUser.username).writeFloat(otherUser.cursorX).writeInt(otherUser.cursorY);
|
||||
}
|
||||
user = users.set(myUUID, new User(socket, username, page));
|
||||
user = users.set(myUUID, new User(socket, username, page, rawURL));
|
||||
sendToAllButSelf(user, createWriter(Endian.LE, 6 + username.length).writeByte(MessageType.ClientJoined).writeUInt(user.id).writeShortString(username).toBuffer());
|
||||
user.send(usersToSend.toBuffer());
|
||||
break;
|
||||
|
@ -86,10 +87,21 @@ server.on("connection", (socket) => {
|
|||
}
|
||||
case MessageType.Ping:
|
||||
{
|
||||
const cursorX = reader.readFloat();
|
||||
const cursorY = reader.readInt();
|
||||
const packet = createWriter(Endian.LE, 9).writeByte(MessageType.Ping).writeFloat(cursorX).writeInt(cursorY).toBuffer();
|
||||
sendToAll(user, packet);
|
||||
if (user === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((Date.now() - user.lastPingReset) >= 1000) {
|
||||
user.allowedPings = 10;
|
||||
}
|
||||
|
||||
if (user.allowedPings > 0) {
|
||||
user.allowedPings--;
|
||||
const cursorX = reader.readFloat();
|
||||
const cursorY = reader.readInt();
|
||||
const packet = createWriter(Endian.LE, 9).writeByte(MessageType.Ping).writeFloat(cursorX).writeInt(cursorY).toBuffer();
|
||||
sendToAll(user, packet);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,20 @@ export default class User {
|
|||
public readonly id:number;
|
||||
public readonly username:string;
|
||||
public readonly currentURL:string;
|
||||
public readonly rawURL:string = "";
|
||||
public cursorX:number = 0;
|
||||
public cursorY:number = 0;
|
||||
public allowedPings:number;
|
||||
public lastPingReset:number;
|
||||
|
||||
constructor(socket:WebSocket, username:string, currentURL:string) {
|
||||
constructor(socket:WebSocket, username:string, currentURL:string, rawURL:string) {
|
||||
this.socket = socket;
|
||||
this.id = User.USER_IDS++;
|
||||
this.username = username;
|
||||
this.currentURL = currentURL;
|
||||
this.rawURL = rawURL;
|
||||
this.allowedPings = 10;
|
||||
this.lastPingReset = Date.now();
|
||||
}
|
||||
|
||||
send(data:Buffer) {
|
||||
|
|
Loading…
Reference in a new issue