Skip to content

Commit

Permalink
feat: 🎸 Backend Websocket server completed
Browse files Browse the repository at this point in the history
  • Loading branch information
yashksaini-coder committed Dec 16, 2024
1 parent 018b641 commit 47bc7e0
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 326 deletions.
46 changes: 46 additions & 0 deletions Backend/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ws_1 = require("ws");
let userCount = 0;
const wss = new ws_1.WebSocketServer({ port: 8080 });
const allSocket = [];
wss.on("connection", (ws) => {
userCount++;
console.log(userCount);
ws.on("message", (data) => {
try {
const info = JSON.parse(data.toString());
if (info.type === "join") {
if (info.payload.roomid == "") {
return;
}
else {
allSocket.push({
socket: ws,
roomid: info.payload.roomid,
});
console.log("user joined the room :" + info.payload.roomid);
}
}
if (info.type === "chat") {
const currentUser = allSocket.find((x) => x.socket == ws);
allSocket.map((e) => {
if (e.roomid == (currentUser === null || currentUser === void 0 ? void 0 : currentUser.roomid)) {
e.socket.send(JSON.stringify({
name: info.payload.name,
message: info.payload.message,
}));
}
});
}
}
catch (error) {
console.log(error);
}
});
ws.on("close", () => {
userCount--;
console.log(userCount);
});
});
console.log("Server is running at port:- ws://localhost:8080");
Loading

0 comments on commit 47bc7e0

Please sign in to comment.