Skip to content

Instantly share code, notes, and snippets.

@talebisinan
Created August 5, 2020 06:58
Show Gist options
  • Save talebisinan/31d2da1726399a6c2ae54c68db2924a4 to your computer and use it in GitHub Desktop.
Save talebisinan/31d2da1726399a6c2ae54c68db2924a4 to your computer and use it in GitHub Desktop.
webrtc public server
/**************/
/*** CONFIG ***/
/**************/
var PORT = 8080;
/*************/
/*** SETUP ***/
/*************/
var express = require("express");
var https = require("https");
var bodyParser = require("body-parser");
var main = express();
const fs = require("fs");
const key = fs.readFileSync("./RootCA.pem");
const cert = fs.readFileSync("./RootCA.crt");
var server = https.createServer({ key: key, cert: cert }, main);
var io = require("socket.io").listen(server);
server.listen(PORT, null, function () {
console.log("Listening on port " + PORT);
});
main.get("/", function (req, res) {
res.sendFile(__dirname + "/client.html");
});
var channels = {};
var sockets = {};
/**
* Users will connect to the signaling server, after which they'll issue a "join"
* to join a particular channel. The signaling server keeps track of all sockets
* who are in a channel, and on join will send out 'addPeer' events to each pair
* of users in a channel. When clients receive the 'addPeer' even they'll begin
* setting up an RTCPeerConnection with one another. During this process they'll
* need to relay ICECandidate information to one another, as well as SessionDescription
* information. After all of that happens, they'll finally be able to complete
* the peer connection and will be streaming audio/video between eachother.
*/
io.sockets.on("connection", function (socket) {
socket.channels = {};
sockets[socket.id] = socket;
console.log("[" + socket.id + "] connection accepted");
socket.on("disconnect", function () {
for (var channel in socket.channels) {
part(channel);
}
console.log("[" + socket.id + "] disconnected");
delete sockets[socket.id];
});
socket.on("join", function (config) {
console.log("[" + socket.id + "] join ", config);
var channel = config.channel;
var userdata = config.userdata;
if (channel in socket.channels) {
console.log("[" + socket.id + "] ERROR: already joined ", channel);
return;
}
if (!(channel in channels)) {
channels[channel] = {};
}
for (id in channels[channel]) {
channels[channel][id].emit("addPeer", {
peer_id: socket.id,
should_create_offer: false,
});
socket.emit("addPeer", { peer_id: id, should_create_offer: true });
}
channels[channel][socket.id] = socket;
socket.channels[channel] = channel;
});
function part(channel) {
console.log("[" + socket.id + "] part ");
if (!(channel in socket.channels)) {
console.log("[" + socket.id + "] ERROR: not in ", channel);
return;
}
delete socket.channels[channel];
delete channels[channel][socket.id];
for (id in channels[channel]) {
channels[channel][id].emit("removePeer", { peer_id: socket.id });
socket.emit("removePeer", { peer_id: id });
}
}
socket.on("part", part);
socket.on("relayICECandidate", function (config) {
var peer_id = config.peer_id;
var ice_candidate = config.ice_candidate;
console.log(
"[" + socket.id + "] relaying ICE candidate to [" + peer_id + "] ",
ice_candidate
);
if (peer_id in sockets) {
sockets[peer_id].emit("iceCandidate", {
peer_id: socket.id,
ice_candidate: ice_candidate,
});
}
});
socket.on("relaySessionDescription", function (config) {
var peer_id = config.peer_id;
var session_description = config.session_description;
console.log(
"[" + socket.id + "] relaying session description to [" + peer_id + "] ",
session_description
);
if (peer_id in sockets) {
sockets[peer_id].emit("sessionDescription", {
peer_id: socket.id,
session_description: session_description,
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment