Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Support for Spacebar WebRTC #1108

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,19 @@
"@spacebar/api": "dist/api",
"@spacebar/cdn": "dist/cdn",
"@spacebar/gateway": "dist/gateway",
"@spacebar/util": "dist/util"
"@spacebar/util": "dist/util",
"@spacebar/webrtc": "dist/webrtc"
},
"optionalDependencies": {
"@yukikaze-bot/erlpack": "^1.0.1",
"jimp": "^1.6.0",
"mysql": "^2.18.1",
"medooze-media-server": "0.129.9",
"nodemailer-mailgun-transport": "^2.1.5",
"nodemailer-mailjet-transport": "github:n0script22/nodemailer-mailjet-transport",
"nodemailer-sendgrid-transport": "github:Maria-Golomb/nodemailer-sendgrid-transport",
"pg": "^8.13.1",
"semantic-sdp": "3.26.0",
"sqlite3": "^5.1.7"
}
}
10 changes: 9 additions & 1 deletion src/bundle/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import http from "http";
import * as Api from "@spacebar/api";
import * as Gateway from "@spacebar/gateway";
import { CDNServer } from "@spacebar/cdn";
import * as Webrtc from "@spacebar/webrtc";
import express from "express";
import { green, bold } from "picocolors";
import { Config, initDatabase, Sentry } from "@spacebar/util";
Expand All @@ -36,12 +37,14 @@ server.on("request", app);
const api = new Api.SpacebarServer({ server, port, production, app });
const cdn = new CDNServer({ server, port, production, app });
const gateway = new Gateway.Server({ server, port, production });
const webrtc = new Webrtc.Server({ server, port, production });

process.on("SIGTERM", async () => {
console.log("Shutting down due to SIGTERM");
await gateway.stop();
await cdn.stop();
await api.stop();
await webrtc.stop();
server.close();
Sentry.close();
});
Expand All @@ -54,7 +57,12 @@ async function main() {
await new Promise((resolve) =>
server.listen({ port }, () => resolve(undefined)),
);
await Promise.all([api.start(), cdn.start(), gateway.start()]);
await Promise.all([
api.start(),
cdn.start(),
gateway.start(),
webrtc.start(),
]);

Sentry.errorHandler(app);

Expand Down
1 change: 1 addition & 0 deletions src/gateway/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class Server {
}

this.server.on("upgrade", (request, socket, head) => {
if (request.url?.includes("voice")) return;
MaddyUnderStars marked this conversation as resolved.
Show resolved Hide resolved
this.ws.handleUpgrade(request, socket, head, (socket) => {
this.ws.emit("connection", socket, request);
});
Expand Down
8 changes: 6 additions & 2 deletions src/gateway/opcodes/VoiceStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema;
const isNew = body.channel_id === null && body.guild_id === null;
let isChanged = false;

let voiceState: VoiceState;
try {
Expand All @@ -54,6 +56,8 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
return;
}

if (voiceState.channel_id !== body.channel_id) isChanged = true;

//If a user change voice channel between guild we should send a left event first
if (
voiceState.guild_id !== body.guild_id &&
Expand Down Expand Up @@ -111,7 +115,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
]);

//If it's null it means that we are leaving the channel and this event is not needed
if (voiceState.channel_id !== null) {
if ((isNew || isChanged) && voiceState.channel_id !== null) {
const guild = await Guild.findOne({
where: { id: voiceState.guild_id },
});
Expand All @@ -134,7 +138,7 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
guild_id: voiceState.guild_id,
endpoint: guildRegion.endpoint,
},
guild_id: voiceState.guild_id,
user_id: this.user_id,
Comment on lines -137 to +141
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??? why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You had guild id twice but missing user id which actually goes there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is incorrect. That parameter is used to determine the recipients of this gateway event. Your edit only sends the event to the user who left the channel, which means other users in the voice channel won't be updated.

} as VoiceServerUpdateEvent);
}
}
4 changes: 2 additions & 2 deletions src/gateway/util/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

// import { VoiceOPCodes } from "@spacebar/webrtc";
import { VoiceOPCodes } from "@spacebar/webrtc";

export enum OPCODES {
Dispatch = 0,
Expand Down Expand Up @@ -63,7 +63,7 @@ export enum CLOSECODES {
}

export interface Payload {
op: OPCODES /* | VoiceOPCodes */;
op: OPCODES | VoiceOPCodes;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
d?: any;
s?: number;
Expand Down
Loading
Loading