-
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.
feat: 🎸 Backend Websocket server completed
- Loading branch information
1 parent
018b641
commit 47bc7e0
Showing
5 changed files
with
151 additions
and
326 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,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"); |
Oops, something went wrong.