Skip to content

Commit

Permalink
Merge pull request #50 from aanu2021/extended-ui-change
Browse files Browse the repository at this point in the history
implemented real time movement of user
  • Loading branch information
sanam2405 authored May 1, 2024
2 parents 4f19da8 + 51654cc commit ff7569b
Show file tree
Hide file tree
Showing 14 changed files with 696 additions and 124 deletions.
72 changes: 72 additions & 0 deletions backend/ws/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.6",
"@types/ws": "^8.5.10",
"axios": "^1.6.8",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
Expand Down
32 changes: 27 additions & 5 deletions backend/ws/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import url from "url";
import axios from "axios";
import { WebSocketServer, WebSocket } from "ws";
import {
INCOMING_MESSAGE,
Expand Down Expand Up @@ -55,7 +56,7 @@ httpServer.on("upgrade", (req, socket, head) => {
});
});

wss.on("connection", function connection(ws: WebSocket, req) {
wss.on("connection", async function connection(ws: WebSocket, req) {
console.log(
`HTTP Server upgraded to WSS Server and is running on PORT ${PORT}`,
);
Expand All @@ -67,9 +68,10 @@ wss.on("connection", function connection(ws: WebSocket, req) {
const token: string = url.parse(req.url, true).query.token || "";
// const userId = extractUserId(token);

const checkIfAuthorized = await tokenIsValid(token);
// Perform auth
if (!token || !tokenIsValid(token)) {
console.log("Unauthorized user");
if (!token || !checkIfAuthorized) {
console.log("Unauthorized user from ws backend");
// Send an unauthorized message to the client
ws.send(
JSON.stringify({
Expand All @@ -95,7 +97,7 @@ wss.on("connection", function connection(ws: WebSocket, req) {
const jsonData = JSON.parse(dataString);
console.log("JSON DATA received from client : ", jsonData);

messageHandler(ws, jsonData);
messageHandler(ws, token, jsonData);
// wss.clients.forEach(function each(client) {
// if (client.readyState === WebSocket.OPEN) {
// client.send(data, { binary: isBinary });
Expand Down Expand Up @@ -131,7 +133,11 @@ wss.on("close", () => {
clearInterval(interval);
});

function messageHandler(ws: WebSocket, message: INCOMING_MESSAGE) {
function messageHandler(
ws: WebSocket,
token: string,
message: INCOMING_MESSAGE,
) {
if (message.type == SUPPORTED_MESSAGES.PONG) {
console.log("Pong received by ws backend");
const payload = message.payload;
Expand Down Expand Up @@ -214,6 +220,22 @@ function messageHandler(ws: WebSocket, message: INCOMING_MESSAGE) {
return;
}

/*
Update the location to the Postgres database
via the ts-backend
*/

// const response = axios.post("localhost:5050/api/addLocation",
// {

// },
// {
// headers : {
// "Authorization": `Bearer ${token}`,
// "Content-Type": "application/json"
// }
// });

const outgoingPayload: OUTGOING_MESSAGE = {
type: OUTGOING_SUPPORTED_MESSAGE.ADD_LOCATION,
payload: {
Expand Down
Loading

1 comment on commit ff7569b

@vercel
Copy link

@vercel vercel bot commented on ff7569b May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.