-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from fahim-tazz/create-rtc-room
Create rtc room
- Loading branch information
Showing
9 changed files
with
1,279 additions
and
8 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 @@ | ||
FROM node:22-alpine3.19 AS builder | ||
WORKDIR /app | ||
|
||
COPY package.json package-lock.json ./ | ||
RUN npm ci | ||
COPY . . | ||
|
||
ENV HOSTNAME="0.0.0.0" | ||
ENV PORT=3003 | ||
|
||
EXPOSE 3003 | ||
|
||
CMD ["npm", "run", "dev"] |
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,44 @@ | ||
const WebSocket = require("ws"); | ||
|
||
const wss = new WebSocket.Server({port: 3003}); | ||
|
||
const rooms = new Map(); | ||
|
||
wss.on("connection", (ws) => { | ||
console.log("New Peer connected!"); | ||
ws.send("Welcome to Signaling Service!") | ||
|
||
let currentRoom; | ||
ws.on("message", (message) => { | ||
const parsedMessage = JSON.parse(message); | ||
if (parsedMessage.type === 'join') { | ||
// Assign the user to the specified room | ||
currentRoom = parsedMessage.room; | ||
if (!rooms.has(currentRoom)) { | ||
rooms.set(currentRoom, []); | ||
} | ||
rooms.get(currentRoom).push(ws); | ||
console.log(`User joined room: ${currentRoom}`); | ||
} else { | ||
// Relay the message to other clients in the same room | ||
if (currentRoom && rooms.has(currentRoom)) { | ||
rooms.get(currentRoom).forEach((client) => { | ||
if (client !== ws && client.readyState === WebSocket.OPEN) { | ||
client.send(message.toString()); // Forward the message | ||
} | ||
}); | ||
} else { | ||
ws.send("Sorry, please join a room first."); | ||
} | ||
} | ||
}); | ||
ws.on('close', () => { | ||
if (currentRoom && rooms.has(currentRoom)) { | ||
const newRoom = rooms.get(currentRoom).filter((client) => client !== ws); | ||
if (newRoom.length === 0) { | ||
rooms.delete(currentRoom); | ||
} | ||
console.log(`User left room: ${currentRoom}`); | ||
} | ||
}); | ||
}) |
Oops, something went wrong.