mirror of
https://github.com/darkwire/darkwire.io.git
synced 2025-07-18 18:54:52 +00:00
Adhoc update
This commit is contained in:
parent
b39367c4b4
commit
4800514b31
71
src/room.js
71
src/room.js
@ -1,7 +1,7 @@
|
|||||||
import _ from 'underscore';
|
import _ from "underscore";
|
||||||
import {EventEmitter} from 'events';
|
import { EventEmitter } from "events";
|
||||||
import util from 'util';
|
import util from "util";
|
||||||
import uuid from 'uuid';
|
import uuid from "uuid";
|
||||||
|
|
||||||
class Room {
|
class Room {
|
||||||
constructor(io = {}, id = {}) {
|
constructor(io = {}, id = {}) {
|
||||||
@ -15,23 +15,20 @@ class Room {
|
|||||||
|
|
||||||
const thisIO = io.of(this._id);
|
const thisIO = io.of(this._id);
|
||||||
|
|
||||||
thisIO.on('connection', (socket) => {
|
thisIO.on("connection", socket => {
|
||||||
let addedUser = false;
|
let addedUser = false;
|
||||||
|
|
||||||
// when the client emits 'new message', this listens and executes
|
// when the client emits 'new message', this listens and executes
|
||||||
socket.on('new message', (data) => {
|
socket.on("new message", data => {
|
||||||
const { username } = socket;
|
const { username } = socket;
|
||||||
const isRateLimited = this.isRateLimited(username);
|
const isRateLimited = this.isRateLimited(username);
|
||||||
|
|
||||||
if (isRateLimited) {
|
if (isRateLimited) {
|
||||||
return thisIO.emit('rated', {
|
return thisIO.emit("rated", { username, id: socket.user.id });
|
||||||
username,
|
|
||||||
id: socket.user.id,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we tell the client to execute 'new message'
|
// we tell the client to execute 'new message'
|
||||||
socket.broadcast.emit('new message', {
|
socket.broadcast.emit("new message", {
|
||||||
username,
|
username,
|
||||||
id: socket.user.id,
|
id: socket.user.id,
|
||||||
message: data.message,
|
message: data.message,
|
||||||
@ -39,12 +36,14 @@ class Room {
|
|||||||
data: data.data,
|
data: data.data,
|
||||||
vector: data.vector,
|
vector: data.vector,
|
||||||
secretKeys: data.secretKeys,
|
secretKeys: data.secretKeys,
|
||||||
signature: data.signature,
|
signature: data.signature
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('add user', (data) => {
|
socket.on("add user", data => {
|
||||||
if (addedUser) { return; }
|
if (addedUser) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
data.id = uuid.v4();
|
data.id = uuid.v4();
|
||||||
|
|
||||||
@ -59,57 +58,53 @@ class Room {
|
|||||||
addedUser = true;
|
addedUser = true;
|
||||||
|
|
||||||
// Broadcast to ALL sockets, including this one
|
// Broadcast to ALL sockets, including this one
|
||||||
thisIO.emit('user joined', {
|
thisIO.emit("user joined", {
|
||||||
username: socket.username,
|
username: socket.username,
|
||||||
numUsers: this.numUsers,
|
numUsers: this.numUsers,
|
||||||
users: this.users,
|
users: this.users
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// when the client emits 'typing', we broadcast it to others
|
// when the client emits 'typing', we broadcast it to others
|
||||||
socket.on('typing', () => {
|
socket.on("typing", () => {
|
||||||
socket.broadcast.emit('typing', {
|
socket.broadcast.emit("typing", { username: socket.username });
|
||||||
username: socket.username
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// when the client emits 'stop typing', we broadcast it to others
|
// when the client emits 'stop typing', we broadcast it to others
|
||||||
socket.on('stop typing', () => {
|
socket.on("stop typing", () => {
|
||||||
socket.broadcast.emit('stop typing', {
|
socket.broadcast.emit("stop typing", { username: socket.username });
|
||||||
username: socket.username
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// when the user disconnects.. perform this
|
// when the user disconnects.. perform this
|
||||||
socket.on('disconnect', () => {
|
socket.on("disconnect", () => {
|
||||||
if (addedUser) {
|
if (addedUser) {
|
||||||
--this.numUsers;
|
--this.numUsers;
|
||||||
this.users = _.without(this.users, socket.user);
|
this.users = _.without(this.users, socket.user);
|
||||||
|
|
||||||
// echo globally that this client has left
|
// echo globally that this client has left
|
||||||
socket.broadcast.emit('user left', {
|
socket.broadcast.emit("user left", {
|
||||||
username: socket.username,
|
username: socket.username,
|
||||||
numUsers: this.numUsers,
|
numUsers: this.numUsers,
|
||||||
users: this.users,
|
users: this.users,
|
||||||
id: socket.user.id,
|
id: socket.user.id
|
||||||
});
|
});
|
||||||
|
|
||||||
// remove room from rooms array
|
// remove room from rooms array
|
||||||
if (this.numUsers === 0) {
|
if (this.numUsers === 0) {
|
||||||
this.emit('empty');
|
this.emit("empty");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update user
|
// Update user
|
||||||
socket.on('update user', (data) => {
|
socket.on("update user", data => {
|
||||||
const newUsername = this.sanitizeUsername(data.newUsername);
|
const newUsername = this.sanitizeUsername(data.newUsername);
|
||||||
|
|
||||||
if (newUsername.length > 16) {
|
if (newUsername.length > 16) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = _.find(this.users, (users) => {
|
const user = _.find(this.users, users => {
|
||||||
return users === socket.user;
|
return users === socket.user;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -117,14 +112,12 @@ class Room {
|
|||||||
socket.username = user.username = newUsername;
|
socket.username = user.username = newUsername;
|
||||||
socket.user = user;
|
socket.user = user;
|
||||||
|
|
||||||
thisIO.emit('user update', {
|
thisIO.emit("user update", {
|
||||||
username: socket.username,
|
username: socket.username,
|
||||||
id: socket.user.id,
|
id: socket.user.id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,9 +132,12 @@ class Room {
|
|||||||
|
|
||||||
triggerRateLimitOn(username) {
|
triggerRateLimitOn(username) {
|
||||||
this.addToRateQueue(username);
|
this.addToRateQueue(username);
|
||||||
setTimeout(() => {
|
setTimeout(
|
||||||
|
() => {
|
||||||
this.removeFromRateQueue(username);
|
this.removeFromRateQueue(username);
|
||||||
}, 120);
|
},
|
||||||
|
120
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
addToRateQueue(username) {
|
addToRateQueue(username) {
|
||||||
@ -155,7 +151,7 @@ class Room {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sanitizeUsername(str) {
|
sanitizeUsername(str) {
|
||||||
return str.replace(/[^A-Za-z0-9]/g, '-');
|
return str.replace(/[^A-Za-z0-9]/g, "-");
|
||||||
}
|
}
|
||||||
|
|
||||||
roomId() {
|
roomId() {
|
||||||
@ -166,3 +162,4 @@ class Room {
|
|||||||
util.inherits(Room, EventEmitter);
|
util.inherits(Room, EventEmitter);
|
||||||
|
|
||||||
export default Room;
|
export default Room;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user