good progress with server and client back and forth

- getting there slowly with console logs and understanding the steps along the way
This commit is contained in:
talksik
2022-04-28 08:51:34 -05:00
parent f38436ae0c
commit 44ee03c9e5
6 changed files with 436 additions and 20 deletions
+6 -1
View File
@@ -6,6 +6,7 @@ import { UserService } from "./services/user.service";
import { UserStatus } from "@nirvana/core/models";
import cors from "cors";
import getConversationRoutes from "./routes/conversation";
import getRtcRoutes from "./routes/rtc";
import getSearchRoutes from "./routes/search";
import getUserRoutes from "./routes/user";
@@ -23,12 +24,16 @@ app.get("/", (req: Request, res: Response) => {
res.send("hello world.");
});
app.use("/api/rtc", getRtcRoutes());
app.use("/api/user", getUserRoutes());
app.use("/api/search", getSearchRoutes());
app.use("/api/conversations", getConversationRoutes());
const PORT = 5000;
var server = app.listen(PORT, () => console.log("express running"));
var server = app.listen(PORT, () =>
console.log(`express running on port ${PORT}`)
);
// socket IO stuff
+2 -1
View File
@@ -12,7 +12,8 @@
"google-auth-library": "^7.14.0",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.4.1",
"socket.io": "^4.4.1"
"socket.io": "^4.4.1",
"wrtc": "^0.4.7"
},
"scripts": {
"dev": "NODE_ENV=development nodemon",
+78
View File
@@ -0,0 +1,78 @@
import express, { Application, Request, Response } from "express";
const webrtc = require("wrtc");
export default function getRtcRoutes() {
const router = express.Router();
router.use(express.json());
// get user details based on id token
router.post("/join/:lineId", handleJoin);
return router;
}
// a mapping between a certain line and the streams for it
const linesStreams: {
[lineId: string]: any[];
} = {};
async function handleJoin(req: Request, res: Response) {
try {
const { lineId } = req.params;
const { sdp } = req.body;
const peer = new webrtc.RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.stunprotocol.org",
},
],
});
// allow this peer connection to receive other peoples' streams
peer.ontrack = (e: any) => handleStreams(e, peer, lineId);
// allow this peer connection for this specific line to get tracks for this line already
linesStreams[lineId]?.forEach((stream: any) => {
stream.getTracks().forEach((track: any) => {
console.log(`have tracks in this line`, track, stream);
peer.addTrack(track, stream);
});
});
// create connection between server and client who hit this endpoint
const desc = new webrtc.RTCSessionDescription(sdp);
await peer.setRemoteDescription(desc);
const answer = await peer.createAnswer();
await peer.setLocalDescription(answer);
const payload = {
sdp: peer.localDescription,
};
res
.status(200)
.json({ message: `You can now broadcast to ${lineId}!`, data: payload });
} catch (error) {
console.log(error);
res.status(500).json({
message: "Problem in setting up peer connection with media server",
error,
});
}
}
function handleStreams(e: any, peer: any, lineId: string) {
// if the room/line exists already, then add to the list of streams
if (lineId in linesStreams) {
const newStreamsForLine = [...(linesStreams[lineId] ?? []), ...e.streams];
linesStreams[lineId] = newStreamsForLine;
} else {
// create a new list of streams
linesStreams[lineId] = [...e.streams];
}
}