-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fd673f
commit f3f27a1
Showing
14 changed files
with
684 additions
and
415 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || (function () { | ||
var ownKeys = function(o) { | ||
ownKeys = Object.getOwnPropertyNames || function (o) { | ||
var ar = []; | ||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; | ||
return ar; | ||
}; | ||
return ownKeys(o); | ||
}; | ||
return function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
})(); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const static_1 = __importDefault(require("@fastify/static")); | ||
const view_1 = __importDefault(require("@fastify/view")); | ||
const websocket_1 = __importDefault(require("@fastify/websocket")); | ||
const dotenv = __importStar(require("dotenv")); | ||
const ejs_1 = __importDefault(require("ejs")); | ||
const fastify_1 = require("fastify"); | ||
const path_1 = __importDefault(require("path")); | ||
const routes_1 = __importDefault(require("./routes")); | ||
const cron_1 = __importDefault(require("./utils/cron")); | ||
const handleWebsocket_1 = __importDefault(require("./utils/handleWebsocket")); | ||
dotenv.config(); | ||
const server = (0, fastify_1.fastify)({ logger: true }); | ||
const PORT = process.env.PORT ? +Number.parseInt(process.env.PORT) : 3000; | ||
(async () => { | ||
try { | ||
(0, cron_1.default)(); | ||
//serve static | ||
await server.register(static_1.default, { | ||
root: path_1.default.resolve('./public'), | ||
}); | ||
server.register(view_1.default, { | ||
engine: { | ||
ejs: ejs_1.default, | ||
}, | ||
root: 'public', | ||
}); | ||
server.register(routes_1.default); | ||
//websocket | ||
server.register(websocket_1.default); | ||
server.register(async function (fastify) { | ||
fastify.get('/ws', { websocket: true }, (0, handleWebsocket_1.default)(server.log)); | ||
}); | ||
server.listen({ port: PORT, host: '0.0.0.0' }, (err) => { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
console.log(`::: -> Server listening at http://localhost:${PORT}`); | ||
}); | ||
} | ||
catch (err) { | ||
server.log.error(err); | ||
process.exit(1); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const room_router_1 = __importDefault(require("./room.router")); | ||
exports.default = async (fastify, opts, done) => { | ||
fastify.get('/', async function (req, reply) { | ||
await reply.view('home'); | ||
}); | ||
await fastify.register(room_router_1.default, { prefix: '/room' }); | ||
done(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = roomRoutes; | ||
const rooms_1 = __importDefault(require("../utils/rooms")); | ||
function roomRoutes(fastify, opts, done) { | ||
fastify.post('/:id', (req, reply) => { | ||
reply.send(rooms_1.default.getInstance().new(req.params.id)); | ||
}); | ||
fastify.get('/:id', (req, reply) => { | ||
const secretKey = req.query.k; | ||
const thisRoom = rooms_1.default.getInstance().get(req.params.id); | ||
if (!thisRoom || thisRoom.getSecretKey() !== secretKey) { | ||
return reply.redirect('/'); | ||
} | ||
reply.view('call', { roomId: req.params.id }); | ||
}); | ||
done(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = startCron; | ||
const node_cron_1 = __importDefault(require("node-cron")); | ||
const rooms_1 = __importDefault(require("./rooms")); | ||
function startCron() { | ||
node_cron_1.default.schedule('*/5 * * * *', () => { | ||
rooms_1.default.getInstance() | ||
.getRoomsData() | ||
.forEach((room, key) => { | ||
const isEmptyMember = room.getMember().length === 0; | ||
const hasNotBeenUpdatedSinceFiveMinutesAgo = new Date().getTime() - room.getUpdatedAt().getTime() >= 5 * 60 * 1000; | ||
if (hasNotBeenUpdatedSinceFiveMinutesAgo && isEmptyMember) | ||
rooms_1.default.getInstance().delete(key); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = handleWS; | ||
const wsMessage_1 = __importDefault(require("./wsMessage")); | ||
const rooms_1 = __importDefault(require("./rooms")); | ||
const user_1 = require("./user"); | ||
const wsClients_1 = __importDefault(require("./wsClients")); | ||
function handleWS(log) { | ||
return function (connection, request) { | ||
var _a; | ||
const { peerId, roomId, name } = request.query; | ||
//add to client cache & room | ||
const newUser = new user_1.User(peerId, name, connection.socket); | ||
wsClients_1.default.getInstance().add(peerId, newUser); | ||
const beforeUpdateRoomData = [ | ||
...(((_a = rooms_1.default.getInstance().get(roomId)) === null || _a === void 0 ? void 0 : _a.getMemberWithoutSocket()) || []), | ||
]; | ||
rooms_1.default.getInstance().add(roomId, newUser); | ||
connection.socket.send(JSON.stringify(new wsMessage_1.default('join_room', beforeUpdateRoomData))); | ||
connection.socket.on('message', (messageRaw) => { | ||
var _a, _b, _c, _d, _e; | ||
// eslint-disable-next-line @typescript-eslint/no-base-to-string | ||
const { type, message } = JSON.parse(messageRaw.toString()); | ||
switch (type) { | ||
case 'microphone': | ||
(_a = wsClients_1.default.getInstance().get(peerId)) === null || _a === void 0 ? void 0 : _a.setMicrophone(Boolean(message)); | ||
(_b = rooms_1.default.getInstance() | ||
.get(roomId)) === null || _b === void 0 ? void 0 : _b.boardcast(new wsMessage_1.default('microphone', { peerId, value: message })); | ||
break; | ||
case 'camera': | ||
(_c = wsClients_1.default.getInstance().get(peerId)) === null || _c === void 0 ? void 0 : _c.setCamera(Boolean(message)); | ||
(_d = rooms_1.default.getInstance() | ||
.get(roomId)) === null || _d === void 0 ? void 0 : _d.boardcast(new wsMessage_1.default('camera', { peerId, value: message })); | ||
break; | ||
case 'message': | ||
log.info({ type: 'message', msg: JSON.stringify({ roomId, peerId, message }) }); | ||
(_e = rooms_1.default.getInstance() | ||
.get(roomId)) === null || _e === void 0 ? void 0 : _e.boardcast(new wsMessage_1.default('message', { peerId, value: message })); | ||
break; | ||
default: return; | ||
} | ||
}); | ||
connection.socket.on('close', () => { | ||
var _a, _b; | ||
//remove client cache & delete from room | ||
try { | ||
(_a = rooms_1.default.getInstance().get(roomId)) === null || _a === void 0 ? void 0 : _a.removeMember(peerId); | ||
wsClients_1.default.getInstance().delete(peerId); | ||
(_b = rooms_1.default.getInstance().get(roomId)) === null || _b === void 0 ? void 0 : _b.boardcast(new wsMessage_1.default('disconnect', peerId)); | ||
} | ||
catch (err) { | ||
let message = 'Unknown Error'; | ||
if (err instanceof Error) | ||
message = err.message; | ||
log.info({ type: 'out_room', msg: message }); | ||
} | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Room = void 0; | ||
const uuid_1 = require("uuid"); | ||
class Room { | ||
constructor(id) { | ||
this.member = []; | ||
this.secretKey = (0, uuid_1.v4)(); | ||
this.id = id; | ||
this.updatedAt = new Date(); | ||
this.createdAt = new Date(); | ||
} | ||
getId() { | ||
return this.id; | ||
} | ||
getSecretKey() { | ||
return this.secretKey; | ||
} | ||
getMember() { | ||
return this.member; | ||
} | ||
getMemberWithoutSocket() { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
return this.member.map((_a) => { | ||
var { socket } = _a, rest = __rest(_a, ["socket"]); | ||
return rest; | ||
}); | ||
} | ||
addMember(user) { | ||
this.updatedAt = new Date(); | ||
return this.member.push(user); | ||
} | ||
removeMember(id) { | ||
this.updatedAt = new Date(); | ||
this.member = this.member.filter(({ peerId }) => peerId !== id); | ||
return this.member; | ||
} | ||
boardcast(message) { | ||
const listClients = this.member; | ||
listClients === null || listClients === void 0 ? void 0 : listClients.forEach((client) => client.socket.send(JSON.stringify(message))); | ||
} | ||
getCreatedAt() { | ||
return this.createdAt; | ||
} | ||
getUpdatedAt() { | ||
return this.updatedAt; | ||
} | ||
} | ||
exports.Room = Room; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const room_1 = require("./room"); | ||
class Rooms { | ||
constructor() { | ||
this.roomsData = new Map(); | ||
} | ||
new(roomID) { | ||
if (this.roomsData.has(roomID)) | ||
throw new Error('This room is exists'); | ||
const newRoom = new room_1.Room(roomID); | ||
this.roomsData.set(roomID, newRoom); | ||
return newRoom; | ||
} | ||
getRoomsData() { | ||
return this.roomsData; | ||
} | ||
add(roomID, user) { | ||
var _a; | ||
if (!this.roomsData.has(roomID)) | ||
throw new Error('This room is not exists'); | ||
return (_a = this.roomsData.get(roomID)) === null || _a === void 0 ? void 0 : _a.addMember(user); | ||
} | ||
get(roomID) { | ||
return this.roomsData.get(roomID); | ||
} | ||
delete(roomID) { | ||
if (!this.roomsData.has(roomID)) | ||
throw new Error('This room is not exists'); | ||
return this.roomsData.delete(roomID); | ||
} | ||
static getInstance() { | ||
if (!this.instance) { | ||
this.instance = new Rooms(); | ||
} | ||
return this.instance; | ||
} | ||
} | ||
exports.default = Rooms; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.User = void 0; | ||
class User { | ||
constructor(peerId, name, socket) { | ||
this.name = name; | ||
this.peerId = peerId; | ||
this.openCamera = true; | ||
this.openMicrophone = true; | ||
this.socket = socket; | ||
} | ||
setCamera(openCamera) { | ||
this.openCamera = openCamera; | ||
} | ||
setMicrophone(openMicrophone) { | ||
this.openMicrophone = openMicrophone; | ||
} | ||
} | ||
exports.User = User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class WsClients { | ||
constructor() { | ||
this.data = new Map(); | ||
} | ||
add(id, client) { | ||
return this.data.set(id, client); | ||
} | ||
get(id) { | ||
return this.data.get(id); | ||
} | ||
delete(id) { | ||
if (!this.data.has(id)) | ||
throw new Error('This user is not exists'); | ||
this.data.delete(id); | ||
} | ||
static getInstance() { | ||
if (!this.instance) { | ||
this.instance = new WsClients(); | ||
} | ||
return this.instance; | ||
} | ||
} | ||
exports.default = WsClients; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class WSMessage { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
constructor(type, message) { | ||
this.type = type; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
this.message = message; | ||
} | ||
} | ||
exports.default = WSMessage; |
Oops, something went wrong.