mpp-server-dev2/src/Client.js

105 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-06-06 19:22:43 +02:00
const Room = require("./Room.js");
require('node-json-color-stringify');
class Client extends EventEmitter {
constructor(ws, req, server) {
super();
EventEmitter.call(this);
this.user;
this.connectionid = server.connectionid;
this.server = server;
this.participantId;
this.channel;
this.ws = ws;
this.req = req;
2019-06-09 21:05:52 +02:00
this.ip = (req.connection.remoteAddress).replace("::ffff:", "");
this.destroied = false;
2019-06-06 19:22:43 +02:00
this.bindEventListeners();
require('./Message.js')(this);
}
isConnected() {
return this.ws && this.ws.readyState === WebSocket.OPEN;
}
isConnecting() {
return this.ws && this.ws.readyState === WebSocket.CONNECTING;
}
setChannel(_id, settings) {
if (this.channel && this.channel._id == _id) return;
2019-06-06 19:22:43 +02:00
if (this.server.rooms.get(_id)) {
2019-06-11 23:01:44 +02:00
let room = this.server.rooms.get(_id);
let userbanned = room.bans.get(this.user._id);
if (userbanned && (Date.now() - userbanned.bannedtime >= userbanned.msbanned)) {
room.bans.delete(userbanned.user._id);
userbanned = undefined;
}
if (userbanned) {
console.log(Date.now() - userbanned.bannedtime)
room.Notification(this.user._id,
"Notice",
`Currently banned from \"${_id}\" for ${Math.ceil(Math.floor((userbanned.msbanned - (Date.now() - userbanned.bannedtime)) / 1000) / 60)} minutes.`,
7000,
"",
"#room",
"short"
);
return;
}
2019-06-06 19:22:43 +02:00
let channel = this.channel;
if (channel) this.channel.emit("bye", this);
if (channel) this.channel.updateCh();
this.channel = this.server.rooms.get(_id);
this.channel.join(this);
} else {
let room = new Room(this.server, _id, settings);
this.server.rooms.set(_id, room);
if (this.channel) this.channel.emit("bye", this);
this.channel = this.server.rooms.get(_id);
this.channel.join(this);
}
}
sendArray(arr) {
if (this.isConnected()) {
2019-06-11 23:01:44 +02:00
//console.log(`SEND: `, JSON.colorStringify(arr));
2019-06-06 19:22:43 +02:00
this.ws.send(JSON.stringify(arr));
}
}
destroy() {
this.ws.close();
2019-06-09 21:05:52 +02:00
if (this.channel) {
this.channel.emit("bye", this)
}
2019-06-06 19:22:43 +02:00
this.user;
this.participantId;
this.channel;
2019-06-11 23:01:44 +02:00
this.server.roomlisteners.delete(this.connectionid);
2019-06-06 19:22:43 +02:00
this.connectionid;
this.server.connections.delete(this.connectionid);
2019-06-09 21:05:52 +02:00
this.destroied = true;
2019-06-06 19:22:43 +02:00
console.log(`Removed Connection ${this.connectionid}.`);
}
bindEventListeners() {
this.ws.on("message", (evt) => {
try {
2019-06-09 21:05:52 +02:00
let transmission = JSON.parse(evt);
2019-06-06 19:22:43 +02:00
for (let msg of transmission) {
if (!msg.hasOwnProperty("m")) return;
if (!this.server.legit_m.includes(msg.m)) return;
this.emit(msg.m, msg);
2019-06-11 23:01:44 +02:00
//console.log(`RECIEVE: `, JSON.colorStringify(msg));
2019-06-06 19:22:43 +02:00
}
2019-06-09 21:05:52 +02:00
} catch (e) {
console.log(e)
this.destroy();
2019-06-06 19:22:43 +02:00
}
});
this.ws.on("close", () => {
2019-06-09 21:05:52 +02:00
if (!this.destroied)
2019-06-11 23:01:44 +02:00
this.destroy();
2019-06-06 19:22:43 +02:00
});
this.ws.addEventListener("error", (err) => {
console.error(err);
2019-06-09 21:05:52 +02:00
if (!this.destroied)
2019-06-11 23:01:44 +02:00
this.destroy();
2019-06-06 19:22:43 +02:00
});
}
}
module.exports = Client;