Skip to content

Commit

Permalink
added auto-leaving
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowLp174 committed Sep 9, 2022
1 parent 32bca2f commit 2f5b7f2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
7 changes: 1 addition & 6 deletions Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,6 @@ class MediaPlayer extends Media {
this.disconnect(false, false);
this.emit("finish");
}
cleanUp() { // TODO: similar to disconnect() but doesn't kill existing processes
this.paused = false;
this.currBuffer = null;
this.currTime = "00:00:00";
}
pause() {
if (this.paused) return;
this.paused = true;
Expand Down Expand Up @@ -271,7 +266,7 @@ class MediaPlayer extends Media {
]);
}
#setupFmpeg() {
this.ffmpeg.on("exit", async (c, s) => {
this.ffmpeg.on("exit", async (_c, s) => {
if (s == "SIGTERM") return; // killed intentionally
this.#ffmpegFinished();
});
Expand Down
31 changes: 27 additions & 4 deletions Revoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class VoiceConnection {
this.setupSignaling();
this.signaling.connect(channelId);

//this.uid = Revoice.uid();
this.leaveTimeout = opts.leaveOnEmpty;
this.leaving; // the actual timeout cancellable

this.media = null;

this.eventemitter = new EventEmitter();
Expand Down Expand Up @@ -52,22 +54,42 @@ class VoiceConnection {

// user events
signaling.on("roomfetched", () => {
this.initLeave();
signaling.users.forEach((user) => {
this.voice.users.set(user.id, user);
});
});
signaling.on("userjoin", (user) => {
this.voice.users.set(user.id, user);
if (this.leaving) {
clearTimeout(this.leaving);
this.leaving = null;
}
this.emit("userjoin", user);
});
signaling.on("userleave", (user) => {
const old = this.voice.users.get(user.id);
old.connected = false;
old.connectedTo = null;
this.voice.users.set(user.id, old);
this.initLeave();
this.emit("userleave", user);
});
}
initLeave() {
const signaling = this.signaling;
if (this.leaving) {
clearTimeout(this.leaving);
this.leaving = null;
}
if (!(signaling.roomEmpty && this.leaveTimeout)) return;
this.leaving = setTimeout(() => {
this.leave();
this.once("leave", () => {
this.destroy();
});
}, this.leaveTimeout * 1000);
}
initTransports(data) {
this.sendTransport = this.device.createSendTransport({...data.data.sendTransport});
this.sendTransport.on("connect", ({ dtlsParameters }, callback) => {
Expand Down Expand Up @@ -127,7 +149,7 @@ class VoiceConnection {
this.disconnect();
if (this.media) await this.media.destroy();
res();
})
});
}
async leave() {
await this.disconnect();
Expand Down Expand Up @@ -217,7 +239,7 @@ class Revoice {
return this.users.has(id);
}

join(channelId) {
join(channelId, leaveIfEmpty=false) { // leaveIfEmpty == amount of seconds the bot will wait before leaving if the room is empty
return new Promise((res, rej) => {
this.api.get("/channels/" + channelId).then(data => {
if (data.channel_type != "VoiceChannel") return rej(Revoice.Error.NOT_A_VC);
Expand All @@ -230,7 +252,8 @@ class Revoice {

const connection = new VoiceConnection(channelId, this, {
signaling: signaling,
device: device
device: device,
leaveOnEmpty: leaveIfEmpty
});
connection.updateState(Revoice.State.JOINING);
this.connections.set(channelId, connection);
Expand Down
23 changes: 18 additions & 5 deletions Signaling.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ const { WebSocket } = require("ws");
const User = require("./User.js");

class Signaling {
constructor(apiClient, channelId) {
constructor(apiClient, channelId, reconnectTimeout=3000) {
this.client = apiClient;
this.channelId = channelId;
this.reconnectTimeout = reconnectTimeout;

this.eventemitter = new EventEmitter();
this.currId = -1;
this.reconnecting = false;

this.users = [];
this.roomEmpty = null;

return this;
}
Expand Down Expand Up @@ -39,6 +42,10 @@ class Signaling {
this.ws.close(1000);
this.currId = -1;
}
reconnect() {
this.reconnecting = true;
this.connect(this.channelId);
}

initWebSocket(data) {
this.ws = new WebSocket("wss://vortex.revolt.chat"); // might need to whitelist this in your antivirus
Expand All @@ -52,6 +59,10 @@ class Signaling {
});
this.ws.on("close", (e) => {
if (e !== 1000) console.log("WebSocket Closed: ", e);
// TODO: Reconnect
setTimeout(() => {
this.reconnect();
}, this.reconnectTimeout);
});
this.ws.on("error", (e) => {
console.log("Signaling error: ", e);
Expand All @@ -64,14 +75,15 @@ class Signaling {
processWS(data) { // data == parsed websocket message
switch(data.type) {
case "InitializeTransports":
this.eventemitter.emit("initTransports", data);
if (!this.reconnecting) this.eventemitter.emit("initTransports", data);
this.fetchRoomInfo().then(() => {
this.roomEmpty = (this.users.length == 1);
this.emit("roomfetched");
});
break;
case "Authenticate":
// continue in signaling process
this.eventemitter.emit("authenticate", data);
if (!this.reconnecting) this.eventemitter.emit("authenticate", data);
const request = {
id: ++this.currId,
type: "InitializeTransports",
Expand All @@ -83,7 +95,7 @@ class Signaling {
this.ws.send(JSON.stringify(request));
break;
case "ConnectTransport":
this.eventemitter.emit("ConnectTransport", data);
if (!this.reconnecting) this.eventemitter.emit("ConnectTransport", data);
break;
case "StartProduce":
this.eventemitter.emit("StartProduce", data);
Expand All @@ -106,6 +118,7 @@ class Signaling {
case "UserLeft":
const id = data.data.id;
const removed = this.removeUser(id);
this.roomEmpty = (this.users.length == 1);
this.emit("userleave", removed);
default:
// events like startProduce or UserJoined; will be implemented later
Expand Down Expand Up @@ -139,7 +152,7 @@ class Signaling {
this.ws.send(JSON.stringify(request));
this.on("roominfo", (data) => {
const users = data.data.users;
if ((Object.keys(users).length - 1) == 0) return res();
//if ((Object.keys(users).length - 1) == 0) return res();
let promises = [];
for (let userId in users) {
let user = new User(userId, this.client);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "revoice.js",
"version": "0.1.71",
"version": "0.1.72",
"description": "A voice module for Revolt",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 2f5b7f2

Please sign in to comment.